可能重复:
Integer wrapper objects share the same instances only within the value 127?
How != and == operators work on Integers in Java?意外的结果时,比较整形
我想两个整数与下列情况比较,得到了意想不到的结果
当我做了下面的事情翼,@@@印。
class C { static Integer a = 127; static Integer b = 127; public static void main(String args[]){ if(a==b){ System.out.println("@@@"); } } }
当我做了以下,@@@没有打印。
class C { static Integer a = 145; static Integer b = 145; public static void main(String args[]){ if(a==b){ System.out.println("@@@"); } } }
谁能告诉我可能是什么原因。
如果有人能格式化it..I我不能...... [整数包装对象的 – Rookie
可能重复只能在共享相同的情况下,值127?](http://stackoverflow.com/questions/5117132/integer-wrapper-objects-share-the-same-instances-only-within-the-value-127)和[New Integer vs valueOf]( http://stackoverflow.com/questions/2974561)和[不一致的行为对java的==](http://stackoverflow.com/questions/1148805)和[整数包装对象共享相同的实例,只有值127?] (http://stackoverflow.com/questions/5117132)和... –
更多详细信息,如果你有兴趣http://vanillajava.blogspot.co。uk/2012/01 /令人惊讶的结果 - autoboxing.html –