The interpretation of when the underlying Integer and Double wrapper class to create objects

public void method1() {
    Integer i = new Integer(1);
    Integer j = new Integer(1);
    System.out.println(i == j);
    Integer m = 1;
    Integer n = 1;
    System.out.println(m == n);//True
    Integer x = 128;
    And Integer = 128 ;
    System.out.println(x == y);//False
}

If that, when the value equals or exceeds 128, the creation of two objects address is not the same, for this reason have to see Integer source

1 public static Integer valueOf(int i) {
2     if (i >= IntegerCache.low && i <= IntegerCache.high)//-128~127
3         return IntegerCache.cache[i + (-IntegerCache.low)];
4     return new Integer(i);
5 }

This IntegerCache array is an array of cache wrapper class that you have created, stored inside the integer [-128,127], and when the value at the time of this range, the value directly from this array, when the value is not in this range, it will create a new the object, the address will be different

-------------------------------------------------

So what type Double

    public static void main(String[] args) {
        double a = 2.0;
        double b = 2.0;
        Double c = 2.0;
        Double d = 2.0;
        System.out.println(a == b);//true
        System.out.println(c == d);//false
        System.out.println(a == d);//true
    }

Into the source code

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

Obviously, Double Integer no bells and whistles, just call it create a new object

Guess you like

Origin www.cnblogs.com/yangxusun9/p/12178093.html