背景:
自己测试了一下int型数组和char型数组,发现一个输出的是地址值,一个输出的是字符。
一:代码如下:
public class DebugText2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr=new int[] {1,2,3,4,5};
System.out.println(arr);
char [] arr1=new char[] {'a','b'};
System.out.println(arr1);
}
}
二:输出结果如下:
[I@449b2d27
ab
三:出现去别的原因:
本来都应该是引用型变量,但是Java把他们区分开了,int型数组调用的方法与char型不同。
int型数组调用的方法,还是保留地址值
public void println(Object x) {
String s = String.valueOf(x);
if (getClass() == PrintStream.class) {
// need to apply String.valueOf again since first invocation
// might return null
writeln(String.valueOf(s));
} else {
synchronized (this) {
print(s);
newLine();
}
}
}
char型数组调用的方法,使得地址值转化为变量
public void println(char[] x) {
if (getClass() == PrintStream.class) {
writeln(x);
} else {
synchronized (this) {
print(x);
newLine();
}
}
}