Here is the snippet of the code
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 output
abcaa
[I@239d5fe6
[Ljava.lang.Integer;@5527f4f9
I know it has to deal with toString()
method. It has been Overridden in char to return the value. hence we are getting the first output as expected
here is the overridden toString()
method of java.lang.Character
..
public String toString() {
char buf[] = {value};//The value of the Character.
return String.valueOf(buf);
}
But looking at Integer there is also the overridden toString()
method
public String toString() {
return String.valueOf(value); //The value of the Integer.
}
Then why printing a1 and b code calls the default toString()
implementation of the Object class, that is:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Also since valueOf makes another Object but then it's common in both the overridden methods.