Why printing Integer array Object gives hash code while char array object gives value in java? -
here snippet of 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 output
abcaa [i@239d5fe6 [ljava.lang.integer;@5527f4f9
i know has deal tostring()
method. has been overridden in char return value. hence getting first output expected here overridden tostring()
method of java.lang.character
..
public string tostring() { char buf[] = {value};//the value of character. return string.valueof(buf); }
but looking @ integer there overridden tostring()
method
public string tostring() { return string.valueof(value); //the value of integer. }
then why printing a1 , b code calls default tostring()
implementation of object class, is:
public string tostring() { return getclass().getname() + "@" + integer.tohexstring(hashcode()); }
also since valueof makes object it's common in both overridden methods.
because there dedicated method printing char arrays:
https://docs.oracle.com/javase/7/docs/api/java/io/printstream.html#println(char[])
public void println(char[] x)
prints array of characters , terminate line. method behaves though invokes
print(char[])
,println()
.parameters:
x
- array of chars print.
implementation:
public void println(char x[]) { synchronized (this) { print(x); newline(); } }
it has nothing tostring
implementation of char[]
class.