Java中Integer/Long/Double/Float大小比较

在开发的过程中,在jdk1.5后可以直接用==来做比较,后来发现不是这个样子的哈哈~
先看一段代码与运行结束:

    public static void main(String[] args) {
        Integer i1 = 1;
        Integer i2 = 1;
        System.out.println( "i1 == i2:" + (i1 == i2));

        Long l1 = 1l;
        Long l2 = 1l;
        System.out.println("l1 == l2:" + (l1 == l2));


        Double d1 = 1d;
        Double d2 = 1d;
        System.out.println("d1 == d2:" + (d1 == d2));

        Float f1 = 1f;
        Float f2 = 1f;
        System.out.println("f1 == f2:" + (f1 == f2));

    }

这里写图片描述
当看到这个结果,当然是没有问题的。我们再来看一下别一段代码与结果:

        Integer i1 = 200;
        Integer i2 = 200;
        System.out.println( "i1 == i2:" + (i1 == i2));

        Long l1 = 200l;
        Long l2 = 200l;
        System.out.println("l1 == l2:" + (l1 == l2));


        Double d1 = 200d;
        Double d2 = 200d;
        System.out.println("d1 == d2:" + (d1 == d2));

        Float f1 = 200f;
        Float f2 = 200f;
        System.out.println("f1 == f2:" + (f1 == f2));

这里写图片描述
这次全是false时,这时发现Integer的比较不能使用==,那么可以用equals可以进行比较吗?我们可以试一下:

        Integer i1 = 200;
        Integer i2 = 200;
        System.out.println( "i1 == i2:" + (i1.equals(i2)));

        Long l1 = 200l;
        Long l2 = 200l;
        System.out.println("l1 == l2:" + (l1.equals(l2)));


        Double d1 = 200d;
        Double d2 = 200d;
        System.out.println("d1 == d2:" + (d1.equals(d2)));

        Float f1 = 200f;
        Float f2 = 200f;
        System.out.println("f1 == f2:" + (f1.equals(f2)));

这里写图片描述
ok,没有问题,它们是可以用equals进行比较的。那么为什么Integer/Long为1时,可以使用==比较,当为200时不能使用呢?我们看一下源代码:

    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

看到这里我们可以很明白了,在jdk1.5后有自动封箱与拆箱后,当Integer/Long值为-128到127时它会自动从缓存中从一值,这时两个变量的其是一个对象的两个引用。所以在比较的时候结果为true,当大于127时结果为false。我们看一下Double与Float的valueOf方法代码:

    public static Double valueOf(double d) {
        return new Double(d);
    }

Double的valueOf方法是直接new出了一个新的对象。所以我们在以后的比较过程最好使用equals。
如果Long与Integer比较,那么只能是先转成一个类型才能进行比较。


PS:技术有限,如有不对之处,欢迎大家指出。

猜你喜欢

转载自blog.csdn.net/bj_ameng/article/details/53517758