大家 先看这个文章 :
http://hi.baidu.com/gabe2008/blog/item/0a96542c16f5973f359bf7d8.html
public class Myclass {
public static Integer starting;
public static void methodA(){
Integer i = new Integer(25);
starting = i;
methodB(i);
}
private static void methodB(Integer i2){
i2 = i2.intValue();
if(i2==starting) System.out.println("1 true"); //按理说,i2==starting 在128之内能自动解开的。
if(i2.equals(starting))System.out.println("2 true");
else {
System.out.println("false");
}
}
public static void main(String[] args) {
methodA();
}
}
http://hi.baidu.com/gabe2008/blog/item/0a96542c16f5973f359bf7d8.html
public class Myclass {
public static Integer starting;
public static void methodA(){
Integer i = new Integer(25);
starting = i;
methodB(i);
}
private static void methodB(Integer i2){
i2 = i2.intValue();
if(i2==starting) System.out.println("1 true"); //按理说,i2==starting 在128之内能自动解开的。
if(i2.equals(starting))System.out.println("2 true");
else {
System.out.println("false");
}
}
public static void main(String[] args) {
methodA();
}
}
解决方案 »
- 新手上路,求高手帮助!!高分!!!
- 很简单的几个题,请教下各位大哥
- 一道不明白的Java题
- 菜鸟求教啥叫未经检查或不安全的操作?
- Spring调用
- 大家帮帮忙,,,我怎么在打开JSP页面时,将hide隐藏域中的值赋给html中的img标记中的src中的值。感谢
- JBuilder5不支持此程序,不知道什么原因,请各位哥哥姐姐多多指教,不胜感激!!!
- 从数据库中下载文件时,为什么第一次弹出的另存窗体入链接页面的本身,选择打开后,第二个页面才是准备下载的文件另存窗体!
- 100分,快拿。
- 求大神解答!参数传递问题
- 如何截取网页(百度音乐)中代码的问题
- ----------------大家在项目里面都用什么做报表-------------------
输出:1 true
2 true
在Integer在自动装箱时对于值从-128~127之间的值,它们被装箱为Integer对象后,会在内存中被重用,实际上是参考至同一个对象(即a与b指向同一个对象)。如果超过了-128~127之间的值,被装箱后的Integer对象并不会被重用,即相当于每次装箱时都新建了一个Integer对象,
是一个new出来的Integer对象。
如果Integer i=25。那么应该能输出
1 true
2 true
a==b的条件是两个类的引用相等,而不是他们的intValue()相等,
因此,想达到目的,可采用intValue方法比较他们的值相等。初学者,不知理解对不对
所以i2 = i2.intValue();这一句时,自动包装机制将25打包成一个新的Integer对象返回,所以i2==starting的值是false
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
static final int high;
static final Integer cache[]; static {
final int low = -128; // high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
} private IntegerCache() {}
}