I’ve got two arrays:
char[] chars = { '1', '2', '3' };
int[] numbers = { 1, 2, 3 };
Why after calling System.out.print(chars) I’m getting 123 while after System.out.print(numbers) I’ve got smth like [C@9304b1 ?
What is more, after printing System.out.print("abc" + chars) I’m also getting abc[C@9304b1 .
I know that [C@9304b1 equals chars.toString() method but why sometimes System.out.print print only its elements?
PrintStream, the type ofSystem.out, has several overloads for theprintmethod, one of which takes an array of characters (char[]):Thus, in your first example, you get
123printed. However,PrintStreamdoesn’t have an overload forprintthat can accept anint[]as an argument, thus, you end up invokingprint(Object), which will use thetoStringmethod of anObject, consisting of its type and its hashcode.In order to print an
int[], you can useArrays.toString()instead.