×

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

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes 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.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.