×

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java.

The initialization of string array in the java by using the NEW (it creates the object for the class)operator with an array initialize.

The string array in java is used to store the fixed number of strings.

The java main method also contains the String argument as:

Public static void main (String args[])

The initialization of string array to empty?

 We know that the array size can't be changed.

The syntax for empty array:

 private static final String[] EMPTY_ARRAY = new String[0];

The java string array is the collection of objects in the array.

In java, arrays are written dynamically. For creating an array, we use two methods. The first one is creating without the size, and the second one is creating with a size.

The syntax for creating the sized array:

String [] s1 = new String [‘Apple’, ‘Banana’, ‘Mango’];

We may perform different operations on String array, i.e., insertion, deletion, updating, sorting, searching etc...

Let's see the syntax for creating a string array without size.

String [] S1;

We can also create any string with another method.

String s1[];

But above one is the standard way. In the above syntax, strings are null values, and the s1 value, which we declared String [3], places three null values.

The compiler will take the default value stored as the null value.

As we all know that the array size is fixed. But we can change the size of the array after being created dynamically. The ArrayList method changes the size by increasing or decreasing it.

  • Let's see the syntax for ArrayList:
List<String> ArrayList = new ArrayList<>(Arrays.asList(“apple”, “banana”, “mango”));
System.out.println(ArrayList);
  • We can create a string array using an instance variable. The following syntax will create a new string with the default value.
   String [] array = (String []) Array.newInstance(String.class,2);
   System.out.println(Arrays.toString(arr));

Examples

Let's see a simple program for initializing the String Array. Declaring a variable of String [] and initializing the set of strings by using the assignment operator, we can assign the strings to the String [] class.

Example1:

import java.io.*;
public class Demo2
{
public static void main(String args[])
{
String letters[] = {"j", "a", "v", "a", "t" , "p" ,"o" , "i" , "n", "t"};
for(String letter: letters)
{
System.out.println(letter);
}
}
}

Output:

How to Initialize String Array in Java

Hence this is a simple program for initializing the string array.

Example 2: Let's see another easiest method to declare the string array:

import java.io.*;
public class Demo2
{
public static void main(String args[])
{
String animals[] = new String[5];
animals[0] = "cat";
animals[1] = "dog";
animals[2] = "rabbit";
animals[3] = "lion";
animals[4] = "tiger";
for(String animal: animals)
{
System.out.println(animal);
}
}
}

Output:

How to Initialize String Array in Java

The above two programs will have the same values. But if you use the equals method, it will exit with false. Because equals methods don't see the content, it shows the reference based on the program.

Let's see how the string array will be converted into the String

The Arrays.toString() method will convert the String array to the string function. We can't directly implement it. We can use the string conversion to represent the string function.

Majorly there are many ways to initialize the string array. We will discuss a few of them.

Example 3:

Import java.io.*;
public class Demo2 
{
    public static void main(String args[]) 
{
        String[] myArr;
        myArr = new String[5];
        myArr[0] = "First";
        
        for (int i = 0; i < myArr.length; i++)
{
            System.out.println(myArr[i]);
        }
    }
}

Output:

How to Initialize String Array in Java

Overview of String Array in Java

  • The java string array is the collection of objects in the array.
  • In java, arrays are written dynamically. For creating an array, we use two methods. The first one is creating without the size, and the second one is creating with a size.
  • The default string value is sored as null values.
  • The string array in java is used to store the fixed number of strings.

Related Topics

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

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

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Java Boolean parseBoolean() Method

The parseBoolean() method of Boolean class returns a Boolean value for the specified String argument. It returns true if and only if string’s value is equal to “true”, else it...

1 minute 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.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.