Here is a snippet of code
public class Test1 {
public static void main(String[] args) {
char[] a={'a','b','c',97,'a'};
System.out.println(a);
int[] a1={8,6,7};
System.out.println(a1);
Integer[] b={10,20,30};
System.out.println(b);
}
}
Here is the conclusion
abcaa
[I@239d5fe6
[Ljava.lang.Integer;@5527f4f9
I know that he must deal with the method toString(). It was overridden in char to return a value. therefore, we get the first result, as expected here - an overridden method toString() java.lang.Character..
public String toString() {
char buf[] = {value};
return String.valueOf(buf);
}
But looking at Integer, there is also an overridden method toString()
public String toString() {
return String.valueOf(value);
}
Then why print codes a1 and b calls the default implementation toString()of the Object class, namely:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Also, since valueOf creates another object, but then it is common to both overridden methods.
source
share