All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Print String Array In Java

Last Updated : Mar 11, 2024

How To Print String Array In Java

In this article we will show you the solution of how to print string array in java, as usual you need to import package, in case you using eclipse then each program will import itself. We know array contains multiple values with same data type.

To display or print array values you need to iterate each of them, for that reason you need loop help. That will iterate all values and displaying at terminal.

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

Here we defined main class ‘Print_prgm’, which contains public static main method.

To create string variable with initialized values you have to disclose earlier tutorial. There we provide in detail steps with example. First we created string array sArr with three values.

Then for iteration we used for loop and there we iterated and printed one by one at terminal.

//Random Number Generation
package java_prgms;
class Print_prgm
{
 public static void main(String args[])
    {
        String sArr[]={"Dhanu","Hari","Madhu"};
        for(int i=0;i<sArr.length;i++){
         System.out.println(sArr[i]);
        }
    }
}
  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. In main class ‘Print_prgm’, we provide main method definition. There you need to create string data type array variable 'sArr'.
  3. Which is initialized with same data type values '{"Dhanu","Hari","Madhu"}', if you want to give any other strings you just need to modify them accordingly then you will get error-free output.
  4. Next you need to define for loop, which is divided into three sections. First 'i=0' - denotes the initialization, next one is conditional statement that will check the statement each time process if its fails then the loop get stopped. Third one is increment step.
  5. In loop the variable 'i' refers index of the array values through this step we can print the values on terminal.
  6. Note without loop you cannot process the array values.
  7. Within loop we defined println() method that prints each array value at each iteration.
  8. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  9. 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 print string array values in java. To compile java program you need to tell the java compiler which program to compile through ‘javac Print_prgm.java’.

After successful compilation you need to execute ‘java Print_prgm‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output ‘Dhanu Hari Madhu' of array values one by one.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪