×

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

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.