How to declare a String array in java

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

Declare String array in java

There are 2 ways to declare String array in java.

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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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:

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.

You can also use variable name before [].

Without Using new operator

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

Splitting declaration and initialization of String array

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

Using Arrays.fill() to initialize String array

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

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.

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.

Output:

[] []

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

Related Posts

  • Escape double quotes in String in Java
    19 January

    How to escape double quotes in String in java

    In this post, we will see how to escape double quotes in String in java. There are scenarios where you need to escape double quotes already present in the String. This generally happens while dealing with JSON file format or reading file data. Escape double quotes in java Double quotes characters can be escaped with […]

  • 24 December

    How to capitalize first letter in java

    Learn about how to capitalize first letter in java.

  • 06 December

    Difference between replace() and replaceAll() in java

    In this post, we will see difference between String’s replace() and replaceAll() methods in java. String’s replace() and replaceAll() both replace all occurences in the String. String’s replace() takes either two chars or two CharSequences as arguments and it will replace all occurrences of char or String but replaceAll() method takes regex String as argument […]

  • 24 April

    Add character to String in java

    In this post, we will see how to add character to String in java. There are multiple ways to add character to String. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Add character to the start of String You […]

  • 24 March

    Initialize List of String in java

    In this post, we will see how to initialize List of String in java. Can you initialize List of String as below: [crayon-60923c92e131a744986259/] You can't because List is an interface and it can not be instantiated with new List(). 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for […]

  • 29 February

    Java long to String

    In this post, we will see how to convert long to String in java. There are lot of ways to convert long to String.Let’s see each one by one. Using Long.toString() You can use Long class toString() method to convert long to String. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our […]

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.