int, Integer and Integer.valueOf links

First look at an actual essay questions written in java

 1 public class test {
 2      public static void main(String[] arg){
 3         int i1=1;
 4         Integer i2=1;
 5         Integer i3=new Integer(1);
 6         Integer i4=Integer.valueOf(1);
 7         Integer i5=Integer.valueOf(1);
 8         System.out.println(i1==i2);   //true
 9         System.out.println(i2==i3);   //false
10         System.out.println(i2==i4);   //true
11         System.out.println(i3==i4);   //false
12         System.out.println(i4==i5);   //true
13     }
14 } 

1, Integer is a wrapper class int, int is a java basic data types 
can be used after 2, Integer variables to be instantiated, but does not require the variable int 
3, Integer object is actually referenced, when a new Integer, actually generates a pointer to the object; and int is directly stored data value 
4, Integer default is null, a default value is 0 int

Compare with regard to the Integer and int

1, since the variable Integer Integer object is actually a reference to it by two new Integer variables is always generated are not equal (because of a new generation of two objects, which different memory addresses).

1 Integer i = new Integer(100);
2 Integer j = new Integer(100);
3 System.out.print(i == j); //false

2, when comparing int Integer variables and variables, as long as the two variables are like, the result is true (when compared to packaging because int Integer and basic data types, java int is automatically unpacked, and then compared, in fact it becomes comparing two int variables)

1 Integer i = new Integer(100);
2 int j = 1003 System.out.print(i == j); //true

3, new generation of non-variable and new Integer Integer () when comparing the generated variable, the result is false. (Because the new generation of non-variable points Integer object java constant pool, the new Integer () variable generated new objects on the heap point, the two different addresses in memory)

1 Integer i = new Integer(100);
2 Integer j = 100;
3 System.out.print(i == j); //fals

4, two non-Integer object for the new generation, when compared, if two variables interval between -128 to 127, the comparison result is true, if the value of two variables is not in this range, the comparison result is false

1 Integer i = 100;
2 Integer j = 100;
3 System.out.print(i == j); //true
1 Integer i = 128;
2 Integer j = 128;
3 System.out.print(i == j); //false

java compiled Integer i = 100; when translated will become Integer i = Integer.valueOf (100);


参考链接:https://www.cnblogs.com/guodongdidi/p/6953217.html

Guess you like

Origin www.cnblogs.com/chaoaishow/p/11545190.html