public static void main(String[] args) { int z1 = 0; int z2 = 0; System.out.println(z1==z2);//TRUE Integer a1 = -129; Integer a2 = -129; System.out.println(a1==a2);// FALSE a1 = -128; a2 = -128; System.out.println(a1==a2);//TRUE a1 = 127; a2 = 127; System.out.println(a1==a2);//TRUE a1 = 128; a2 = 128; System.out.println(a1==a2);// FALSE // IntegerCache.java 缓存了从-128到127之间的所有的整数对象 // 如果值的范围在-128到127之间,它就从高速缓存返回实例。 }
import java.lang.reflect.Field; public class Test318 { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { Class cache = Integer.class.getDeclaredClasses()[0]; Field myCache = cache.getDeclaredField("cache"); myCache.setAccessible(true); Integer[] newCache = (Integer[]) myCache.get(cache); newCache[132] = newCache[133]; int a = 2; int b = a + a; System.out.printf("%d + %d = %d", a, a, b); //2+2=5 } }
时间: 2024-10-11 16:46:35