Several cases where the basic type in Java is compared to the boxed basic type "= ="
Java has a type system that consists of two parts, a primitive type (primitive), such as an int, a double, and a reference type (reference type), such as String, List. Each base type has a corresponding reference type, called the boxed base type (boxed promitive). The boxed base type that corresponds to int, double is an integer, double.
In the Java 1.5 release, automatic boxing and automatic unpacking, automatic boxing and automatic unpacking are what we know as "syntactic sugars," which blur but do not completely erase the difference between the basic type and the boxed base type. There is a real difference between the two types, and it is important to be very clear about which type to use, and to choose between the two types carefully.
There are three main differences between the base type and the boxed type.
- First: The base type has only values, and the boxed base type has a different identity than their value. In other words, two boxed base types can have the same values and different identities.
- Second: The base type has only a fully functional value, and each boxed base type has a non-functional value except that it corresponds to all the functional values of the base type: null.
- Third: The basic type is usually more time-and space-saving than the boxed base type.
If you're not careful with the three points above, the three-point difference will get you into trouble.
Let's look at a simple example program
As follows
package com.wrh.firstpro; Public class EqualsDemo06 {public static void main ( String[] args) {system. Out . println (Mycompare (new Integer (4 ), new Integer (4 )); } public static int mycompare (Integer i1,integer i2) {return i1<i2?-1 :((i1==i2)? 0 : 1 ); }}
The output of the program is:1
But these two integer instances all represent the same value 4, why is the function mycompare return 1, not the 0 we imagined?
The reason for this is as follows: This analysis refers to the effective Java Chinese version of article 49th .
i1<i2performing a calculation on an expression causes the integer instance referenced by first and second to be automatically disassembled, that is, it extracts their base type values. Calculates the action to check whether the first int value produced is less than the second one. The answer is in the negative. The next test is to execute the calculation expression i1==i2 , which performs an identity comparison on two objects. If I1 and I2 refer to an instance of a different integer that represents the same int value, the comparison operation returns false, so this function incorrectly returns: 1;
However, after I decompile the. class file generated by this file, the code is as follows:
Package Com.wrh.firstpro;import Java.io.PrintStream; Public classequalsdemo06{ Public Static void Main(string[] paramarrayofstring) {System. out. println (Mycompare (NewInteger (4),NewInteger (4))); } Public Static int Mycompare(Integer paramInteger1, integer paramInteger2) {returnParamInteger1 = = ParamInteger2?0: Paraminteger1.intvalue () < Paraminteger2.intvalue ()? -1:1; }}
found that the compiler will return i1<i2?-1:((i1==i2)?0:1); become a
0 : paramInteger1.intValue() < paramInteger2.intValue() ? -11;
Therefore, we analyze the results of the run according to the post-compilation code:1 for the following reasons: The identity analysis of two integer instances is not equal, and then the two integer instances are automatically disassembled by Integer.intvalue () to compare the size, Since these two integer instances represent the same numeric value, the resulting 1
Here's how to fix the problem above:
Add two intermediate variables of type int, and perform all the comparison operations on these intermediate variables to avoid the comparison of identities.
publicstaticintmyCompare(Integer i1,Integer i2){ int temp1=i1; int temp2=i2; return temp1<temp2?-1:((temp1==temp2)?0:1); }
Reference to encapsulated base type==The first example of an operator that is almost always wrong: two is a "= =" Comparison with an integer instance of new.
package com.wrh.firstpro;publicclass EqualsDemo02 { publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub Integer i7=new Integer(4); Integer i8=new Integer(4); System.out.println("i7==i8?"+(i7==i8)); System.out.println("i7.equals(i8)?"+i7.equals(i8)); }}
The results of the operation are as follows:
I7==i8?false
I7.equals (i8)? True
The
Decompile code is as follows:
public class equalsdemo02{public static void main (string[] paramarrayofstring) {Integer localInteger1 = new Integer (4 ); Integer localInteger2 = new Integer (4 ); System. out . println (new StringBuilder (). Append ( Span class= "hljs-string" > "I7==i8?" ). Append (LocalInteger1 = = LocalInteger2). toString ()); System. out . println (new StringBuilder (). Append ( Span class= "hljs-string" > "i7.equals (i8)?" ). Append (Localinteger1.equals (LocalInteger2)). toString ()); }}
The reason for this is that the use of "= =" To compare two instances of a new integer type, immediately represents the same numeric value, because the data of the integer type is compared to the same identity, and therefore not equal. The result of equals is the same as we expected.
Summary : when using "= =" To compare two instances of the new integer type, the same value is represented immediately, and is not equal due to the comparison of the data of the integer type.
The second example program: two "= =" Comparison of an integer instance obtained by auto-boxing
Keep looking down.
package com.wrh.firstpro;publicclass EqualsDemo03 { publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub Integer i3=3; Integer i4=3; System.out.println(i3==i4); System.out.println(i3.equals(i4)); }}
The results of the operation are as follows:
True
True
The difference compared to the previous example is that the
Integer i7=new integer (4);
Integer i8=new integer (4);
Replaced by
Integer i3=3;
Integer i4=3;
Results in the application of the program = = comparison, the judgment is equal, that Integer i3=3;Integer i4=3; makes the identity of the i3, I4 the same.
Summary : When comparing two auto-boxed integer instances with "= =", the results are equal as long as they represent the same values, so their identities are the same.
The anti-compilation code for the above example is as follows
publicclass EqualsDemo03{ publicstaticvoidmain(String[] paramArrayOfString) { Integer localInteger1 = Integer.valueOf(3); Integer localInteger2 = Integer.valueOf(3); System.out.println(localInteger1 == localInteger2); System.out.println(localInteger1.equals(localInteger2)); }}
The third example program: An integer instance that comes out of new, a "= =" Comparison of an integer instance that is automatically boxed
The procedure is as follows:
package com.wrh.firstpro;publicclass EqualsDemo07 { publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub Integer i1=1; Integer i2=new Integer(1); System.out.println(i1==i2); System.out.println(i2.equals(i1)); }}
The results of the program run as follows:
False
True
The results are better understood and are no longer analyzed here.
The fourth example program: "= =" is a comparison of operator operations on the left and right sides of an integer instance
Continue to see examples
package com.wrh.firstpro;publicclass EqualsDemo04 { publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub Integer i1=1; Integer i2=2; Integer i3=3; System.out.println(i3==(i1+i2)); System.out.println(i3.equals(i1+i2)); }}
The operation results are as follows
True
True
That is, the comparison above is done after the automatic unpacking and then the numerical size comparison .
Through the anti-compiled code to believe that it can be a good analysis of the answer, the code is as follows
public class equalsdemo04{public static void Main (string[] paramarrayofstring) {Integer localInteger1 = integer. ValueOf(1);Integer LocalInteger2 = integer. ValueOf(2);Integer LocalInteger3 = integer. ValueOf(3);System. out. println(LocalInteger3. Intvalue() = = LocalInteger1. Intvalue() + LocalInteger2. Intvalue());System. out. println(LocalInteger3. Equals(Integer. ValueOf(LocalInteger1. Intvalue() + LocalInteger2. Intvalue())));}}
Summary: the "= =" Operation of the wrapper class is automatically removed when the arithmetic operation is encountered and the comparison of the numerical size represented
A fifth Example program
package com.wrh.firstpro;publicclass EqualsDemo05 { publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub Integer i6=4; Long i5=4L; //System.out.println(i5==i6);//不能编译,Integer类型和Long类型不能用“==”比较 System.out.println(i6.equals(i5)); }}
The result of the operation is:false, this result is better understood
The post-compilation code is as follows:
publicclass EqualsDemo05{ publicstaticvoidmain(String[] paramArrayOfString) { Integer localInteger = Integer.valueOf(4); Long localLong = Long.valueOf(4L); System.out.println(localInteger.equals(localLong)); }}
Summarize
(1) integer.valueof () and Integer.intvalue () are automatically boxed and automatically disassembled in the anti-compilation code.
(2) Apply "= =" to the two new instances of the integer type to compare, immediately represent the same numeric value, because the data of the integer type is compared to the same identity, and therefore not equal
(3) When comparing two auto-boxed integer instances by "= =", the result is equal if the values represented by the two are the same, so their identity is the same.
(4) when compared by equals, as long as the same type (including automatic boxing and unpacking) represents the same value, is equal.
Other types have the same principle as the automatic boxing and unpacking of integer types, which are not mentioned here.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several cases where the basic type in Java is compared to the boxed basic type "= ="