Arrays.toString() Method in Java - Quiz

Total: 5 questions

1. 

What does the Arrays.toString() method do, and what does its result look like?

The static method Arrays.toString() from the java.util.Arrays class returns a string representation of a one-dimensional array: the elements are listed comma-separated and enclosed in square brackets. For example, Arrays.toString(new int[]{1, 4, 6, 3, 8}) returns the string [1, 4, 6, 3, 8]. It is the shortest way to print an array to the console — one call instead of looping over the elements. The method works with an array of any type: it has nine overloads (one for each of the eight primitive types and one for Object[]). Do not forget the import import java.util.Arrays;.

2. 

Why does System.out.println(array) print something like [I@1b6d3586 instead of the array elements, and how is Arrays.toString(array) different?

An array in Java is an object, but it does not override the toString() method. So when you pass an array directly to println(), the default implementation from the Object class kicks in: it prints the type name ([I — a one-dimensional int array), the @ character, and the hash code in hexadecimal. Such a string carries no information about the elements. The Arrays.toString(array) method fixes this: it walks over the elements and builds a readable string like [1, 4, 6, 3, 8]. For two-dimensional arrays the same problem repeats at every nesting level — there you need Arrays.deepToString().

3. 

When should you use Arrays.toString(), and when Arrays.deepToString()?

Arrays.toString() is meant for one-dimensional arrays — it prints the elements of a single level. If you apply it to a two-dimensional array, each nested array again prints as a type and hash code (for example, [I@74a14482), because the elements are themselves arrays. For multidimensional (nested) arrays you need Arrays.deepToString(): it recursively expands all nesting levels and gives a result like [[1, 2, 3], [4, 5, 6]]. The rule is simple: a one-dimensional array — toString(); nested arrays — deepToString().

4. 

How do you convert an array to a string without square brackets, using your own separator?

Arrays.toString() always adds square brackets and commas. If you need your own format (for example, to write it to a file), use String.join(", ", names) for a string array — you get Anna, Boris, Clara without brackets. For an array of primitives, the Stream API does the job: Arrays.stream(array).mapToObj(String::valueOf).collect(Collectors.joining(", ")). The separator can be anything in both cases — a hyphen, a line break, a semicolon.

5. 

What does Arrays.toString() print for an array of your own class objects, and what does the method return if you pass it null?

Arrays.toString() calls toString() on every element of the array. If your class does not override toString(), instead of the field values you will see a list like ClassName@hash-code. Override toString() in your class and the output becomes readable. If you pass null itself instead of an array, Arrays.toString(null) returns the string "null" and throws no exception — which is handy when debugging uninitialized references.

Page 1 of 1