Open In App

Java Program to Write an Array of Strings to the Output Console

Improve
Improve
Like Article
Like
Save
Share
Report

We cannot print array elements directly in Java, you need to use Arrays.toString() or Arrays.deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.

In Java, arrays do not overwrite toString(). When we try write array directly to output console in Java, we get class_name + ‘@’ + hash_code of the array define by Object.toString(). See the below example for a better understanding. 

Example to Print Array of Strings in Java

Below is the implementation of the topic mentioned above:

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = { "Geeks", "for", "Geeks" };
        System.out.println(gfg);
    }
}


Output

[Ljava.lang.String;@5a07e868

Thus, to print a Java array meaningfully, you don’t need to look further because your very own Collection framework provides lots of array utility methods in java.util.Arrays class. Here we have toString() method and deepToString() method to print array in Java.

How to print in Java

Following are the methods to write an array of strings to the output console mentioned below:

  1. Using Arrays.toString()
  2. Using Arrays.deepToString()
  3. Using for loop

1. Using Arrays.toString()

This method is used when you have one dimensional array.

Java




import java.io.*;
import java.util.Arrays;
 
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = { "Geeks", "for", "Geeks" };
        System.out.println(Arrays.toString(gfg));
    }
}


Output

[Geeks, for, Geeks]

Above, we have used the Arrays.toString() method. Simply pass array name as argument in Arrays.toString() and all the elements of the array will get written in the output console.

2. Using Arrays.deepToString()

This method is used when you have to two dimensional array.

Java




import java.io.*;
import java.util.Arrays;
 
class GFG {
    public static void main(String[] args)
    {
        String gfg[][]
            = { { "GeeksforGeeks", "Article Writing" },
                { "Google", "Search Engine" },
                { "Facebook", "Social Media" } };
        System.out.println(Arrays.deepToString(gfg));
    }
}


Output

[[GeeksforGeeks, Article Writing], [Google, Search Engine], [Facebook, Social Media]]

In the above example, we have used Arrays.deepToString() method. This method takes care of writing elements of two dimensional array to output console.

3. Using for loop

In this method, we will access each element of the array and would write it to the output console.

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        String gfg[] = new String[3];
        gfg[0] = "Geeks";
        gfg[1] = "for";
        gfg[2] = "Geeks";
        for (int i = 0; i <= 2; i++) {
            System.out.print(gfg[i] + " ");
        }
    }
}


Output

Geeks for Geeks 

In the above method, we have used for loop() method to access every element of gfg array and write it to the output console.



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads