All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

String Array Declaration In Java

Last Updated : Mar 11, 2024

String Array Declaration In Java

In this article we will show you the solution of string array declaration in java, Java supporting package needs to import at the first line of code, in case you using eclipse then each program have itself. Then main class defined, which contains static main method.

Generally array used to store multiple same data types values into a single variable. To declare array each language will have some prebuilt rule by that you can define successfully.

Step By Step Guide On String Array Declaration In Java :-

Firstly we defined main class ‘Str_dec’, it contains public static method. To declare array in java define the variable type with square brackets and give name of array variable name.

But it has many type of declaration likes declaring with size, creating with object declaration and so on. Later we will see in another topics each one clearly.

//REVERSE ARRAY DECLARATION
package java_prgms;
class Str_dec
{
 public static void main(String args[])
    {
        String sArr[];
    }
}
  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. The class name and program name needs to be same for proceeds the compile and execution successfully. Otherwise you have to provide separate names for execution and compile time that sometime make you confuse, so ensure it.
  3. Here we defined main class ‘Str_dec’, there we defined public static main method. Then ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  4. Within main method we declared empty string array variable ‘sArr’ without default size. That needs to define as per inbuilt rule of syntax.
  5. The syntax is : String(Datatype) sArr(array name)[]; when you following these syntax will give you right declaration otherwise it will throws error on terminal.
  6. If you are keen on any other declaration methods or declare with size or initialized value then prefer our next tutorial that will lead you to learn remaining things.
  7. Then you can store multiple values into the array variable ‘sArr’ with String datatype values, which is needs to define with double quotes between curly braces.
  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 give string array declaration in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Str_dec.java’.

After successful compilation you need to execute ‘java Str_dec‘ line for gets the output. In this example we just provide declaration that is not initialized and printed on terminal, so its not have output.

In case, you wants define any other array variable then change the ‘sArr’ name to some other as per your wish. On eclipse when pressing run button that will not show you any error inside terminal.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪