您的位置:首页 > 产品设计 > UI/UE

Java中1000==1000为false而100==100为true

2016-03-18 22:04 302 查看
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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: