the difference between int and integer
1, integer is the wrapper class int, int is a basic data type of Java
2. The integer variable must be instantiated before it can be used, and the int variable does not need
3, the integer is actually a reference to the object, when new an integer, is actually generated a pointer to this object, and int is directly stored data values
4, the default value of integer is the default value of Null,int is 0
Extended:
About the comparison of integers and int
1. Because the integer variable is actually a reference to an integer object, the two integer variables generated by new are never equal (because new generates two objects with different memory addresses).
1 New Integer (+); 2 New Integer (+); 3 // false
2, the integer variable and the int variable comparison, as long as the value of two variables is equal, the result is true (because the wrapper class integer and the base data type int Compare, Java will automatically unpack to int, then compare, actually become the comparison of two int variable)
1 New Integer (+); 2 int j =3//true
3. The result is false when the non-new generated integer variable is compared to the variable generated by the new Integer (). (Because a non-new integer variable points to an object in a Java constant pool, the new Integer () generates a variable that points to the new object in the heap, and the address in memory is different)
1 New Integer (+); 2 Integer j = +; 3 // false
4, for two non-new integer objects, when compared, if the value of two variables between the interval-128 to 127, then the comparison result is true, if the value of two variable is not in this interval, the comparison result is false
1 Integer i = +; 2 Integer j = +; 3 // true
1 Integer i =n; 2 Integer j =N; 3 // false
For the reason of article 4th:
Java is translated into integer i = integer.valueof (100) when compiling integer i = 100, while the Java API defines the valueOf of the integer type as follows:
1 Public StaticInteger ValueOf (inti) {2 assertIntegercache.high >= 127;3 if(I >= integercache.low && i <=Integercache.high) {4 returnIntegercache.cache[i + (-Integercache.low)];5 }6 return NewInteger (i);7}
Java for the number between 128 to 127, will be cached, the integer i = 127, 127 will be cached, the next time you write the Integer j = 127, will be taken directly from the cache, will not be new
If there is a wrong place, please correct me.
Reference: http://www.cnblogs.com/guodongdidi/p/6953217.html
The difference between int and integer in the "Go" Java