public class test2 { public static void main(String[] args) {
int i = 127;
Integer s = i;
Integer s1= i;
System.out.println(s==s1);
}
}
输出 true。
public class test2 { public static void main(String[] args) {
int i = 128;
Integer s = i;
Integer s1= i;
System.out.println(s==s1);
}
}
输出 false
int i = 127;
Integer s = Integer.valueOf(i);
Integer s1= Integer.valueOf(i);
System.out.println(s==s1);
}
}
然后看下Integer.valueOf(int)的源码,你jdk会发现有缓存对象。