Java provides the wrapper type for each primitive type:
-Original type: boolean,char,byte,short,int,long,float,double
-Package Type: boolean,character,byte,short,integer,long,float,double
Case one:
classAutounboxingtest { Public Static voidMain (string[] args) {Integer a=NewInteger (3); Integer b=3;//automatically boxing 3 into an integer type intc =3; System. out. println (A = = B);//false Two references do not reference the same objectSystem. out. println (A = = c);//true a automatically unboxing into int type and C comparison }}
Case TWO:
Public class Test03 { publicstaticvoid main (string[] args) { 100 a. System. out. println (f1 = = F2); System. out. println (F3 = = f4);} }
F1, F2, F3, and F4 Four variables are integer object references, so the following = = Operation compares values instead of references.
What is the nature of boxing? When we assign an int value to an integer object, the static method of the integer class is called valueof, and if you look at the source code of the valueof, you know what happened.
Public Static Integer valueOf (int i) { if (i >= integercache.low && i <= integercache . High) return integercache.cache[i + (-integercache.low)]; return New Integer (i);}
Integercache is the inner class of the integer whose code is as follows:
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) As required by JLS. * * The cache is initialized on first usage. The size of the cache * May is controlled by the {@code-xx:autoboxcachemax=<size>} option. * During VM initialization, Java.lang.Integer.IntegerCache.high property * is set and saved in the private system Properties in the * Sun.misc.VM class. */ Private Static classIntegercache {StaticFinalintLow =- -; StaticFinalintHigh ; Staticfinal Integer cache[]; Static { //High value is configured by property inth =127; String Integercachehighpropvalue=Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if(Integercachehighpropvalue! =NULL) { Try { inti =parseint (Integercachehighpropvalue); I= Math.max (I,127); //Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value-(-low)-1); } Catch(NumberFormatException nfe) {//If The property cannot is parsed into an int, ignore it.}} High=h; Cache=Newinteger[(high-low) +1]; intj =Low ; for(intK =0; K < Cache.length; k++) Cache[k]=NewInteger (j + +); //range [ -128, 127] must be interned (JLS7 5.1.7)Assert Integercache.high >=127; } PrivateIntegercache () {}}
To put it simply, if the value of an integer literal is between 128 and 127, then the integer object in the constant pool is not new, and the result of F1==F2 in the above interview is true, and the result of F3==F4 is false.
Case THREE:
null &&!username.equals ("")
To verify that the user name is not null and is not an empty string when logged on, it should be written as: Username! = null &&!username.equals (""), the order of the two cannot be exchanged, not the & operator, Because the first condition is not true, the equals of the string cannot be compared, otherwise a nullpointerexception exception is generated.
Case FOUR:
Usage of the In-memory stack (stack), heap, and static region (static area)
Usually we define a variable of a basic data type, a reference to an object, and a field save for a function call to use the in-memory stack space, while the object created by the new keyword and the constructor is placed in the heap space, and the literal in the program (literal) is written as 100, "Hello" And constants are placed in the static zone. Stack space is the fastest, but the stack is small, usually a large number of objects are placed in the heap space, in theory, the entire memory is not used by other processes in the space or even the hard disk virtual memory can be used as a heap space.
New String ("hello");
In the above statement, the variable str is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed in the static area.
Case FIVE:
Math.Round (11.5) equals how much? How much does Math.Round (-11.5) equal?
Rounding is done by adding 0.5 to the parameter and then taking the next rounding. The return value of Math.Round (11.5) is the return value of 12,math.round (-11.5), which is-11.
Case SIX:
Java face question