However I'm wondering if I just got garbage?
No, you got the result of Object.toString()
, which isn't overridden in arrays:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
So it's not garbage, in that it has a meaning... but it's not a particularly useful value, either.
And your bonus question...
How do you correctly print a character array in java?
Call Arrays.toString(char[])
to convert it to a string... or just
System.out.println(hello);
which will call println(char[])
instead, which converts it into a string. Note that Arrays.toString
will build a string which is obviously an array of characters, whereas System.out.println(hello)
is broadly equivalent to System.out.println(new String(hello))