String[] in Java

Sheeraz Gul Oct 12, 2023
  1. Use String[] While Declaring the Main Method in Java
  2. Use String[] to Create an Array of Strings in Java
String[] in Java

String[] means an array holding a fixed number of strings in Java. The most common use of String[] is when we declare the main method like public static void main(String[] args).

This tutorial will demonstrate how to use String[] in Java.

Use String[] While Declaring the Main Method in Java

The main method invokes the code in Java. It is necessary to pass an array of strings as a parameter to the main method; otherwise, it will not work.

Example:

package delftstack;

public class String_Example {
  public static void main(String[] args) {
    System.out.println("This is String[] Example");
  }
}

String[] args is passed as a parameter to the main method so that the code will run properly.

Output:

This is String[] Example

If the main method is not declared with the String[] parameter, the code will not run.

Example:

package delftstack;

public class String_Example {
  public static void main(String args) {
    System.out.println("This is String[] Example");
  }
}

This code will not run because the main method is not declared with String[].

Output:

Error: Main method not found in class delftstack.String_Example, please define the main method as:
    public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Use String[] to Create an Array of Strings in Java

As mentioned above, String[] is used to create arrays of strings.

Example:

package delftstack;
import java.util.Arrays;

public class String_Example {
  public static void main(String[] args) {
    // Two methods of array initialization
    //  Method 1
    String[] Demo_Array1 = new String[] {"Delftstack1", "Delftstack2", "Delftstack3"};
    // Method 2
    String[] Demo_Array2 = {"Delftstack1", "Delftstack2", "Delftstack3"};
    // Print the Arrays
    System.out.println("String[] Array initialization method 1");
    System.out.println(Arrays.toString(Demo_Array1));
    System.out.println("String[] Array initialization method 2");
    System.out.println(Arrays.toString(Demo_Array2));

    // String[] Array initialization after declaration
    String[] Demo_Array3 = new String[3];
    Demo_Array3[0] = "Delftstack1";
    Demo_Array3[1] = "Delftstack2";
    Demo_Array3[2] = "Delftstack3";
    System.out.println("String[] Array initialization after declaration");
    System.out.println(Arrays.toString(Demo_Array3));
  }
}

The code above creates String[] arrays using three different methods.

Output:

String[] Array initialization method 1
[Delftstack1, Delftstack2, Delftstack3]
String[] Array initialization method 2
[Delftstack1, Delftstack2, Delftstack3]
String[] Array initialization after declaration
[Delftstack1, Delftstack2, Delftstack3]

String[] is the most commonly used array type in Java for different operations.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java String