当我们打印数组初始化的引用变量时会发生什么?
int[] it=new int[10];
sop(it);
结果是什么?
int[] it = new int[10];
System.out.println(it);
it是一个对象,因此您正在调用println(Object)( PrintStream) System.out,它toString()在内部调用传递的对象。数组'toString()类似于Object的toString():
getClass().getName() + "@" + Integer.toHexString(hashCode());
所以输出会是这样的:
[I@756a7c99
其中[表示数组的深度,并I指int. 756a7c99是hashCode()作为十六进制数返回的值。
要打印数组,请使用Arrays.toString(),例如:
int[] it = new int[10];
System.out.println(Arrays.toString(it));
输出:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
就像是[I@30c221
这是新数组的内存地址
int[] it=new int[10];
System.out.println(it);
假设sop是System.out.println,它将向您显示该toString方法返回的 String 结果。在这种情况下,它将是类的名称+“@”+哈希码的十六进制。