×

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

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

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

3 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

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

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.