输出char数组是将所有字符输出到控制台,ArrayList也是输出所有元素到控制台,int数组是输出地址
看源码
public void println(char x[]) {
synchronized (this) {
print(x);
newLine();
}
}
一步一步探索进去最后发现
public void write(char cbuf[]) throws IOException {
write(cbuf, 0, cbuf.length);
}
所以char是输出所有字符
而对于对象的话
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
实际是调用对象的toString方法输出,
ArrayList继承的AbstractCollection.class中重写了toString方法,所以会输出所有值,
AbstractCollection.class
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
而int数组会根据Object.toString输出地址
Object.class
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}