All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Print An Array In Java

Last Updated : Mar 11, 2024

How To Print An Array In Java

In this article we will show you the solution of how to print an array in java, for the foremost everyone must import package that will support your code, in case you using eclipse then each program have itself.

The util.* will helps the program to print array without too much effort on terminal. In main we defined main method with example of printing array on terminal.

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

In main class ‘Dis_prgm’, we defined public static method. May everyone be heard before, array values helps to hold multiple values with same data type on single variable.

Here showing example, defined with string data type three values. Note string data type variable or array variable will only allows string data type value initialization.

In case, you are trying to initialize that with integer values then it will disclose error and you cannot execute successfully.

//Print Array
package java_prgms;
import java.util.*;
class Dis_prgm
{
 public static void main(String args[])
    {
        String strArr[]={"Dhanu","Hari","Madhu"};
        System.out.println(Arrays.toString(strArr));
    }
}
  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. Firstly we defined main class ‘Dis_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. Within this we declared string datatype variable ‘strArr’ with three initialized values ‘{"Dhanu","Hari","Madhu"}’.
  4. Likewise you can define some other string values on variable ‘strArr’ and you can change the array variable name as per your wish.
  5. Using println() method we displaying array on terminal, to get error-free output you need to bind ‘Arrays’ with array variable and toString() method.
  6. 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.
  7. The ‘Arrays’ helps to denotes the array type and toString method helps to convert the value as string datatype.
  8. Without them you cannot get expecting output at last so ensure whether you given perfectly.
  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 array in java. To compile java program you need to tell the java compiler which program to compile through ‘javac Dis_prgm.java’.

After successful compilation you need to execute ‘java Dis_prgm‘ line for gets the output. Then you can see output '["Dhanu","Hari","Madhu"]'.

You can change the above shown initialized string values with any other string based on your interest you will get result as per changes you made.

On eclipse just press of run button then you will get output inside terminal.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪