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

29 Oct, 2020
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. 
Java
import java.io.*;  class GFG {    public static void main(String[] args)    {        String gfg[] = { "Geeks", "for", "Geeks" };        System.out.println(gfg);    }}
[Ljava.lang.String;@3d075dc0


Thus, to print Java array in a meaningful way, 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.
Following are the methods to write an array of strings to the output console.
Method 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));    }}
[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.
Method 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));    }}
[[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.
Method 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] + " ");        }    }}
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.
Java-Array-Programs
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Different ways of Reading a text file in Java
Functional Interfaces in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?