int的new对象问题.如下图代码所示:
<span style="font-size:14px;">class Demo_Array {
public static void main(String[] args) {
String a = new String("abc");
String b = "abc";
//-------------------------------
int c = 2;
Integer d = new Integer(2);
Integer f = new Integer(2);
System.out.println(a==b); //false 内存空间不一样 a 是在堆中生成了一个对象
/*
变量a与数值10都是放在栈中而Integer b=new Integer(10) 的引用对象b是放在栈中,Integer(10)是放在堆中。b对象指向堆中的Integer(10)
那为什么输出a==b为true啊? ==判断的是引用地址与内容相等 可是,变量a与引用对象b引用的地址不同啊
*/
System.out.println(c==d); //true 这里实际上是:c == d.intValue()
System.out.println(d==f); //false 这里是因为d与f的地址都不一样
}
}
</span>