If the interviewer asks the difference between an integer and an int: it is estimated that most people will only say two points, Ingeter is the wrapper class for int, and the initial value of int is null for 0,ingeter. But if the interviewer asks again, integer i = 1;int II = 1; is I==ii true or false? It is estimated that some people can not answer the question, if you ask others, it is estimated that more people will have a confused mind. So I summed them up and I hope to be helpful to everyone.
First look at the code:
1 package com.test; 2/** 3 * 4 * @author Liu Ling 5 * 6 */7 public class Testinteger {8 9/**10 * @param ARGS11 */12 public static void Main (string[] args) {int i = 128;14 Integer i2 = 128;15 Integer i3 = new integer,//integer automatically unboxing to int, so true17 System.out.println (i = = i2); Em.out.println (i = = i3), System.out.println ("**************"), Integer i5 = 127;//java at compile time, translated into Integer i5 = integer.valueof (127), Integer I6 = 127;22 System.out.println (i5 = = I6);//TRUE23/* Integer i5 = 128;24 Integer I6 = 128;25 System.out.println (i5 = = I6);//false26 */Integer II5 = new Integer (127); SYSTEM.OUT.PRINTLN (i5 = = II5); False28 Integer i7 = new Integer (+), integer i8 = new Integer (123), System.out.println (i7 = = i8); False31}32}
First, 17 rows and 18 rows of output are true because the integer and int ratios are automatically unboxing (jdk1.5 or more).
The result of 22 rows is true, while 25 rows is false and many people do not move why. In fact, when compiling the integer i5 = 127, Java is translated into integer i5 = integer.valueof (127), so the key is to see the valueOf () function. Just look at the valueof () function of the source code will understand. JDK source code of the valueof function, such as:
Public Static Integer valueOf (int i) {2 assert integercache.high >= 127; 3 If (i >= integercache.low && i <= integercache.high)4 return Integercache.cache[i + (-integercache.low)]; 5 returnnew Integer (i); 6 }
Look at the source code everyone will understand that for the number between 128 to 127 will be cached, Integer i5 = 127, 127 will be cached, the next time you write an Integer I6 = 127, will be directly from the cache, will not be new. So the result of 22 rows is true, while the 25 behavior is false.
For 27 rows and 30 rows, false because the objects are not the same.
My summary of the above situation is as follows:
① Anyway, the integer is not equal to the new integer. Does not go through the unboxing process, the i3 reference points to the heap, and I4 points to his memory (Chang), whose memory address is not the same, so false
② two are non-new integer, if the number is between 128 to 127 is true, otherwise false
Java is translated as Integer i2 = integer.valueof (128) When compiling integer i2 = 128, while the valueOf () function caches the number from 128 to 127
③ two are all new, false.
④int and Integer (regardless of new No) are true because the integer is automatically removed to int and then to the
int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, and integer is the wrapper class provided by Java for Int. The default value for int is 0, and the default value of integer is null, that is, integer can differentiate between unassigned and value 0, and int cannot express an unassigned condition, for example, to express the difference between not taking an exam and a test score of 0, you can only use the integer . In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int is 0, so the result is 0 when displayed in a text box with an El expression, so int does not work together as a type of form data for the Web tier . In Hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined for the int type, It is also necessary to set its Unsaved-value property to 0 in the HBM mapping file. In addition, the integer provides multiple integer-related action methods, such as converting a string to an integer, and also defining constants that represent the maximum and minimum values of integers.
What are the comparisons of the "Java Face question" 5 integer int? Detailed analysis