All TalkersCode Topics

Follow TalkersCode On Social Media

How To Initialize String Array In Java

Last Updated : Jan 1, 2023

How To Initialize String Array In Java

In this article we will show you the solution of how to initialize string array in java, as usual you need to import package, in case you using eclipse then each program have itself.

Then we provide main class, which contains static main method. There we gave example for array initialization with string values as per prebuilt rule. Each language will have separate way to provide variable declaration and initialization.

Step By Step Guide On How To Initialize String Array In Java :-

In main class ‘StrIni_prgm’, we defined public static method. Generally the array variable can hold multiple values with same data type on single variable.

Here shown example, contain three string values in string datatype of array variable ‘str’. Using that program you can create similar one with different string values and do not try with integer values because that is also considered as string data type.

//Array Initialization
package java_prgms;
class StrIni_prgm
 public static void main(String args[])
        String str[]={"Dhanu","Hari","Madhu"};
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. Foremost we defined main class ‘StrIni_prgm’, there we defined public static method. Then ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  3. There you need to provide proper declaration and initialization. So follow this syntax : String(Data Type) str(variable name)[] = {value 1, value 2,..etc};
  4. Now you can understand the above given sample code, if you forget anything that may throws error on terminal otherwise it will does not did.
  5. The array variable ‘str’ have three string values ‘{"Dhanu","Hari","Madhu"}’. You can add extra values as well with them. If you test with integer values into double quotes then it will treat as string and you cannot make any calculation like doing with integers.
  6. To create integer array you have to follow the same rule but will differ in some place that we will see in future tutorials in detail.
  7. When you wants to change array variable name or values then do changes accordingly that will give you successful result inside terminal.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to initialize string array in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’. After successful compilation you need to execute ‘java StrIni_prgm‘ line for gets the output.

In this program we just shown example, so you cannot see any output on terminal.

In case, you are facing any error then ensure all the code line by line. On eclipse just pressing run button will not have to be show output and also error.

I hope this article on how to initialize string array in java helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials