public class TestNoSync { public static void main(String[] args) throws IOException { Integer i1 = 100; Integer i2 = 100; Integer i3 = 130; Integer i4 = 130; Long l1 = 110L; Long l2 = 110L; Byte b1 = 110; Byte b2 = 110; Short s1 = 110; Short s2 = 110; Character c1 = 110; Character c2 = 110; System.out.println(i1 == i2); System.out.println(i3 == i4); System.out.println(l1 == l2); System.out.println(b1 == b2); System.out.println(s1 == s2); System.out.println(c1 == c2); } }
console:
true false true true true true
总结:
- 如果整型字面量的值在-128~127之间,那么不会new出新的Integer对象,而是直接使用常量池中的Integer对象
- Byte,Long,Short,Character也同样适用