码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-03-18 23:31:05      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

    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
        }
}

 

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

标签:

原文地址:http://www.cnblogs.com/AndyHoo/p/5293825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!