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.

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.

Was this post helpful?

Related Posts

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in JavaWas this post helpful? In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix […]

  • Find character in String in java
    02 February

    Find Character in String in Java

    Table of ContentsHow to Find Character in String in Java1. Using indexOf() Method to Find Character in String in Java2. Using lastIndexOf() Method to Find Char in String in JavaFind Character at Index in String in Javahow Do I Find Word in String in Java?Was this post helpful? In this post, we will see how […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusionWas this post […]

  • Java String contains IgnoreCase
    23 June

    Java String contains Ignore Case

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusionWas this post helpful? We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons libraryWas this post helpful? In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.