×

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

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 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 Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

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

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.