char[] c = {'1', '2'};
System.out.println(c);
System.out.println(c + "");
Output:
debug进行源码分析:
System.out.println(c);
public void println(char x[]) { synchronized (this) { print(x); newLine(); } }public void print(char s[]) { write(s); }
System.out.println(c + "");
public StringBuilder() { super(16); }AbstractStringBuilder(int capacity) { value = new char[capacity]; }/*对这一步有点不理解 obj的类型为Object, 但是值为char[] = {1, 2} 难道char[]也能算作一个对象吗*/ public StringBuilder append(Object obj) { return append(String.valueOf(obj));public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }/*注意这一步,Object类的toString并没有返回字符*/ public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
总结
因为运算符【+】的存在,且操作数之一为String(""),导致调用了Object类(char[]属于Objectl类?存疑)的toString方法,返回了class名和hash值,而非字符
遗留问题
System.out.println(c.toString());
的确跳转到了Object类的toString,getClass().getName()返回了【[C】
[C是什么类?char[]吗?