如何让 a == 1 && a == 2 && a == 3同时成立?

「这是我参与11月更文挑战的第22天,活动详情查看:2021最后一次更文挑战

简述

看到这个问题是不是觉得很神奇,其实只要利用一点Java的语法特性就能解决问题。

1、解决思路:

Java专家组发现,Integer对象初始化的大部分数据介于-128127之间,因此Integer对小数据(-128127)预置了缓存,以此来提升大部分情况下实例化一个Integer的性能。其解决办法是,在jvm初始化的时候,数据-128127之间的数字便被缓存到了本地内存中,如果初始化-128127之间的数字,会直接从内存中取出,不需要新建一个对象,而这个数据被缓存在Integer的内部类IntegerCache中。所以我们通过获取Integer内部类IntegerCache,也就是缓存-128~127的类,并将索引为129(也就是1)的对象赋值给130(2)、131(3),此时当我们从Integer中去取值的时候就会发现1==2==3。

2、Integer内部类IntegerCache源码

源码示例:

/**
 *  IntegerCache缓存了-128~127
 */
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) {
            try {
                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);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

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

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}
复制代码

3、具体实现

代码示例:

package com.lizba.p3;

import java.lang.reflect.Field;

/**
 * <p>
 *		a == 1 && a == 2 && a == 3示例代码
 * </p >
 *
 * @Author: Liziba
 * @Date: 2021/6/3 17:19
 */
public class IntegerTest {


    public static void main(String[] args){

        try {
            Class<?>[] declaredClasses = Integer.class.getDeclaredClasses();
            Class<?> integerCache = declaredClasses[0];
            Field f = integerCache.getDeclaredField("cache");
            f.setAccessible(true);
            Integer[] cache = (Integer[]) f.get(integerCache);

            System.out.println(cache[129]);
            System.out.println(cache[130]);
            System.out.println(cache[131]);

            cache[130] = cache[129];
            cache[131] = cache[129];
            Integer a = 1;
            
            // true
            if (a == (Integer) 1 && a == (Integer) 2 && a == (Integer) 3) {
                System.out.println(true);
            }
            
            // true
            if (a == Integer.valueOf(1) && a == Integer.valueOf(2)  && a == Integer.valueOf(3)) {
                System.out.println(true);
            }
            
            // 无输出
            if (a == new Integer(1) && a == new Integer(2) && a == new Integer(3)) {
                System.out.println(true);
            }
            System.out.println(cache[129]);
            System.out.println(cache[130]);
            System.out.println(cache[131]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}
复制代码

查看输出结果

4、注意点

如下三个为什么前两个为true,最后一个为false呢?

// true
if (a == (Integer) 1 && a == (Integer) 2 && a == (Integer) 3) {
    System.out.println(true);
}

// true
if (a == Integer.valueOf(1) && a == Integer.valueOf(2)  && a == Integer.valueOf(3)) {
    System.out.println(true);
}

// 无输出
if (a == new Integer(1) && a == new Integer(2) && a == new Integer(3)) {
    System.out.println(true);
}
复制代码

这个答案我们通过源码来揭晓:

当我们通过new关键字来构造一个Integer时,此时是重新在JVM中实例化的一个对象,因此对象地址是不相等的。

 /**
     * The value of the {@code Integer}.
     *
     * @serial
     */
    private final int value;

    /**
     * Constructs a newly allocated {@code Integer} object that
     * represents the specified {@code int} value.
     *
     * @param   value   the value to be represented by the
     *                  {@code Integer} object.
     */
    public Integer(int value) {
        this.value = value;
    }
复制代码

而当我们使用Integer.valueOf(int),-128~127的值会从IntegerCache中获取,此时返回的是同一个对象,地址是相等的。

// -128~127从IntegerCache中获取
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
复制代码

5、总结

这些知识点都是用来考验一个开发者对JDK特性的熟练程度和平时的积累,有时候要是问上了也是有用的,或者也给我们在实际开发中做数据预热来提升服务的性能,带来了一定的思考价值。