×

How to declare string array in Java

Introduction

Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be stored in the arrays.

To understand the string arrays, we need to know the basics of Java arrays and Java strings.

In this tutorial, we will learn about string array and how to declare a string array in Java.

What is string array ?

String array is an array containing fixed number of string values.

The string is sequence of characters It is called as immutable object, i.e., its values cannot be modified. As, string is considered an object in Java, string array is called array of objects.

 Note: The main() method argument itself accepts string array as argument.

Similar to other arrays in Java, the first element of string array is placed at index 0, second at index 1, and so on.

Declaration of String Array

There are two ways to declare string array in Java:

  1. By specifying the size of array
  2. Without specifying the size of array

Syntax:

//declaring the string array without specifying the size
String[] array_name ;


//declaring the string array with size
String[] array_name = new String [10] ;

In the first type of declaration, we declare string array like a normal variable without mentioning its size. However, to use this array, we need to instantiate it with new keyword or initialize it with the string values. If we do not initialize it will give a compile time error as shown below.

How to declare string array in Java

In the second type of declaration, string array is declared and instantiated using new keyword. Here, the size of array is 10 i.e it can hold ten string values.

When we directly print the declared array, without initializing or adding elements in it, all the null values are printed

Example:

Let’s consider the following example where we declare and initialize the string array in Java.

DeclareString.java

public class DeclareString{
	public static void main(String[] args) {


		//declaring without specifying size
		String[] strArray;


		//declaring with size 3
		String[] strArrayWithSize = new String[3];


		//we cannot print strArray (first array) without initializing it
		//System.out.println(strArray[0]);




		//if we print strArrayWithsize (second array) without initializing 
		//it will print null
		System.out.println(strArrayWithSize[0] + " " + strArrayWithSize[1] + " " + strArrayWithSize[2]);


	}
}

Output:

How to declare string array in Java

In this way, we have learned how to declare a string array in Java.


Related Topics

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.