Puzzle (ABC)
package demo;
public class App {
public static void main( final String[] args ) {
final String letters = "ABC";
final char[] numbers = { '1', '2', '3' };
System.out.println( letters + " easy as " + numbers );
}
}
ABC easy as [C@3764951d
This example was taken from PUZZLE 12: ABC in Java™ Puzzlers: Traps, Pitfalls, and Corner Cases.
“The string concatenation operator is defined to perform string conversion on both of its operands and then to concatenate the resulting strings. String conversion for object references, which include arrays, is defined as follows (JLS 15.18.1.1).”
“If the reference is null
, it is converted to the string "null"
. Otherwise, the conversion is performed as if by an invocation of the toString()
method of the referenced object with no arguments; but if the result of invoking the toString()
method is null
, then the string "null"
is used instead.”
“So what is the behavior of invoking toString()
on a non-null char
array? Arrays inherit the toString()
method from Object
(JLS 10.7).”