Have you tried printing array in Java before? What did you do? just passed array to
println() method and expecting it print its elements? Me too, but surprisingly array despite being
Object and providing
length field, doesn't seem
overriding toString() method from
java.lang.Object class. All it prints is
type@somenumber. This is not at all useful for any one who is interested in seeing whether array is empty or not, if not then what elements it has etc. 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() and
deepToString() method to print array in Java. These methods are
overloaded, much like
System.out.println() method to accept all primitive types, which means a different method is called if you pass boolean array, and a different one is called when you
print integer array. Same is true with
deepToString(), which is used to
print two dimensional array in Java. In this Java array tutorial, we will see examples of printing
string array, integer array, byte array and a two dimensional array in Java. Rest of them are like that, which means by following these examples, you should be able to print
boolean,
char,
short,
float,
double and
long array by your own.