java primitive type cache

    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        System.out.println("a==b:"+(a==b));  //a==b:true
        Integer c = new Integer(127);
        Integer d = new Integer(127);
        System.out.println("a==b:"+(c==d));  //a==b:false
        Integer e = 128 ;
        Integer f = 128;
        System.out.println("a==b:"+(e==f));  //a==b:false
    }

For Byte/Short/Long, it is similar to Integer, in the range of -128 to 127, there is a corresponding cache

To sum up, for the wrapper class corresponding to the above basic types, as long as it is within the cache range, that is, within the range of -128 to 127 (one byte, the range of complement representation),

We can directly use == to judge the equality.

float, double, boolean, char are not cached

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324611777&siteId=291194637