The difference Integer with int

Is a wrapper class Integer int, int initial value is 0, the initial value Integer is null.

When Integer type with the type int variables were compared Size:

static void main public (String [] args) { 
int A = 59;
Integer B = 59;
Integer B1 = 59;
Integer Integer.valueOf = C (59);
Integer = C1 Integer.valueOf (59);
Integer new new D = Integer (59);
System.out.println ( "A with int Integer (whether for new new) A == B" + (A == B));
System.out.println ( "A with int Integer (whether for the new new) A == C "+ (A == C));
System.out.println (" A with int Integer (whether for new new) A == D "+ (A == D));
Integer E new Integer = (59);
System.out.println ( "out of two new variables Inetger == E D" + (D == E));
System.out.println ( "out of the new non-variable Integer (- 128 to 127) B == C "+ (B == C));
System.out.println (" out of the new non-variable Integer (-128 to 127) b == b1 "+ ( b == b1)) ;
System.out.println ( "out of the new non-variable Integer (-128 to 127) C == C1" + (C == C1));
Integer F = 128;
Integer F1 = 128;
Integer Integer.valueOf G = ( 128);
Integer Integer.valueOf G1 = (128);
System.out.println ( "out of the new non-variable Integer (> 127 || <-128) == G F" + (G == F));
the System .out.println ( "out of the new non-variable Integer (> 127 || <-128) F F1 ==" + (F1 == F));
System.out.println ( "out of the new non-variable Integer (> || 127 <-128) G == G1 "+ (G == G1));
Integer H = 128;
Integer Integer.valueOf = I (128);
Integer J = new new Integer (128);
System.out.println ( "Integer Integer H == J with the new new" + (H == J));
System.out.println ( "Integer Integer I == J with the new new" + (J == I));
}

 

 

 

1, Integer type variable == comparison with the variable of type int, no matter whether a variable of type Integer is a new Integer object, as long as their values ​​are equal, all is true. Because when a variable of type Integer comparison with a variable of type int, Integer type variable will be unboxing to int type, just compare values.

2, when two new Integer variables are out of the object reference of type Integer, regardless of whether the values ​​are equal, are false. Because the comparison between the type variable references, the comparison is rather than numeric addresses.

In both cases apply in all cases. No need to consider whether the value of variable Integer> = --128 && <= 127

3, instead of two out of the new instantiated variable Integer, the variable value> = --128 && <= 127, is true. Otherwise false

4, non-new out of the variables Integer value regardless of whether it's just like new Integer variables, comparison are false; because of non-new Integer variable to point to the constant pool of Java objects; new Integer variable to point out in the Java heap new objects

Guess you like

Origin www.cnblogs.com/dloading/p/10945385.html