java Integer 超过127时,比较判断

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014655105/article/details/80771054
public class TestInteger {
public static void main(String[] args) {
Integer s1 = 127, s2 = 127, t1 = 128, t2 = 128;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(t1 == t2);
System.out.println(t1.equals(t2));
System.out.println(t1.intValue() == t2.intValue());
}

}

比较答案:

true
true
false
true
true

引用回答

JVM会自动维护八种基本类型的常量池,int常量池中初始化-128~127的范围,所以当为Integer i=127时,在自动装箱过程中是取自常量池中的数值,而当Integer i=128时,128不在常量池范围内,所以在自动装箱过程中需new 128,所以地址不一样。


猜你喜欢

转载自blog.csdn.net/u014655105/article/details/80771054