Friday, October 24, 2025
HomeLanguagesJavaJava Program to Write an Array of Strings to the Output Console

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

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);
    }
}


Output

[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));
    }
}


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.

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[][]
            = { { "Lazyroar", "Article Writing" },
                { "Google", "Search Engine" },
                { "Facebook", "Social Media" } };
        System.out.println(Arrays.deepToString(gfg));
    }
}


Output

[[Lazyroar, 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] + " ");
        }
    }
}


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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS