How to declare a String array in java

In this post, we will see how to declare String array in java.
[toc]

Declare String array in java

There are 2 ways to declare String array in java.

Declaring a String array without size

String[] myStrArr; // Declaring a String array without size

In this declaration, a String array is declared as any normal variable without size. Before using this array, you need to instantiate it otherwise you might get variable might not have initialized error.

Declaring a String array with size

String[] myStrArr = new String[3]; // Declaring a String array with size

In this declaration, a String array is declared and instantiated at the same time. You can directly use this array, but you will see that myStrArr contains null values as it is not initialized yet.

Let’s see both types with the help of example:

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {

        String[] myStrArr1;       //declared string array without size
        String[] myStrArr2 = new String[3];  //declared string array with size

        //System.out.println(myStrArr1[0]); //If you uncomment this line, then you will get
        // variable might not have been initialized
        System.out.print(Arrays.toString(myStrArr2));
    }
}

Output:

[null, null, null]

How to declare and initialize String array in java

There are multiple ways to declare and initialize array in java.

Using new operator

We can declare and initialize array in java using new operator. We don’t have to provide size in this case.

String[] myStrArr = new String[]{"One","Two","Three"};

You can also use variable name before [].

String myStrArr[] = new String[]{"One","Two","Three"};

Without Using new operator

We can also declare and initialize array in java without using new operator.

String[] myStrArr = {"One","Two","Three"};

Splitting declaration and initialization of String array

We can also declare a String array and can initialize it later.

String[] myStrArr1; 
myStrArr1 = new String[]{"One","Two","Three"};

Using Arrays.fill() to initialize String array

We can also declare a String array and can initialize it later.

import java.util.Arrays;
public class Main
{   
    public static void main(String args[]) 
    {
        String[] myStrArr = new String[7];

        // put value "One" to each array element
        Arrays.fill(myStrArr, "One");

        // put value "Two" from index 3, inclusive, to index 5, exclusive
        Arrays.fill(myStrArr, 3, 5, "Two");

        System.out.println(Arrays.toString(myStrArr));
    }
}

Output:

[One, One, One, Two, Two, One, One]

Using Java 8’s setAll() method to initialize String array

We can also use Java 8’s setAll() method and pass a generator to initialize String array in java.

package org.arpit.java2blog;

import java.util.Arrays;

public class StringArrayMain {

    public static void main(String[] args) {
        String[] strArr = new String[7];
        Arrays.setAll(strArr, str -> "One");
        System.out.println(Arrays.toString(strArr));
    }
}

Output:

[One, One, One, One, One, One, One]

How to declare and initialize an empty String array

There are two ways to create empty String array in java.


import java.util.Arrays;
public class Main
{   
    public static void main(String args[]) 
    {
        //1st
        String[] myStrArr1 = new String[0];
        System.out.println(Arrays.toString(myStrArr1)); 

        // 2nd
        String[] myStrArr2 = {};
       System.out.println(Arrays.toString(myStrArr2)); 
    }
}

Output:

[]
[]

That’s all about how to declare a String array in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.