问题一:下面调用什么方法?打印结果是什么?
int[] arr = new int[]{
1,2};
System.out.println(arr);
打印结果:[I@a57993
调用PrintStream的println(Object x)重载方法,除了参数为char[]的其他数组参数都会调用这个方法
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
再看String.valueOf()也有很多的重载方法
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
obj为int[]数组,而所有类型的数组又没有重写toString()方法,默认调用Object的toString()方法,返回的是内存地址值。
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();