Detailed analysis of Java comparison problems
The comparison problem in Java is a very basic and confusing problem. Today, I will summarize and sort out several error prone points in detail, hoping to be helpful to everyone's study and interview.
1、 = = difference from equals()
First, we need to know the difference between = = and equals(). The = = sign always compares the address value. For basic data types, the = = comparison is actually whether the variable values are equal, while for reference data types, the comparison is the address value. Special attention should be paid here to the string type. It is easy to assume that = =, which is easy to make mistakes. The equals () method is a method in the object class. We know that all classes in Java will inherit the object class by default, so class objects will have the equals () method. The equals() method in the object class is shown in the following figure:
It can be seen from the source code that the equals () method in the object class also uses = =, so it actually compares the address value. So if we want to use the equals () method for other comparisons, we need to override the equals () method.
II Basic data type and its packing class
As we all know, byte, short, int, long, Boolean, char, double and float are basic data types, and their declared variables are stored in stack memory. The variables defined by their corresponding wrapper types (byte, short, integer, long, Boolean, character, double) exist in heap memory. For basic data types, their comparison is relatively simple, that is, to judge whether they are equal, use = =, and to compare the size, use <, >, < =, > =. For the type of packaging, there are some differences.
First, check the execution results of the following codes to determine whether they are equal:
The above implementation results are described as follows:
First, for the new method, declare an integer or long object. Because the new object opens up a space in the heap, even if the two values are the same, for = =, the address value is compared, so false will be returned. For wrapper classes of basic data types, the equals () method is overridden to compare the value size, so the equals () method can be used to judge according to the value size. For the comparison between integer variables and int variables, it will be found that the comparison value is also obtained based on the value. This is because the integer type is automatically unpacked and converted to int type during comparison. The explanations of the first three points are applicable to all packaging types. For the direct assignment method, the two integer variables with the value of 48 are judged to be true with the = = sign, but when the value is 128, it is false. This is because at the bottom, N1 = 48 for integer; This direct assignment method actually calls integer Value() method. We can take a brief look at integer The source code of the value () method is shown in the following figure:
We can see that there is an if judgment. When the input I is within the range of [- 128127], it is returned directly from the integercache array. Therefore, for the values within this range, the returned address values are the address values corresponding to this array, so the = = sign will return true. The objects that are not in this range are new objects, so false will be returned. This conclusion holds true for byte, short, integer and long types (if you are interested, you can see the source code of their corresponding value () method). Because the range of byte types is [- 128127], for byte types, using = = is no different from equals().
For size comparison, there is no problem using >, <, < =, > = and they will unpack automatically. However, we generally recommend using the following two methods for size comparison:
Call the xxxvalue () method to convert it into a basic data type for comparison, and use the CompareTo () method for comparison. In the wrapper class, the CompareTo () method is overridden. Looking at the CompareTo () source code, we can see that in fact, it is used at the bottom through automatic unpacking to convert it into the corresponding basic data type for comparison.
2、 Comparison of Java objects
With the above introduction, Object comparison is easier. The principle is the same.
1. Comparison of string types
It should be noted that string types cannot be used directly >, < =, > =, <, and compilation exceptions will be reported.
2. Comparison of class objects
The comparison conclusion of class objects is the same, but it is more complex than basic data types and string types.
To judge whether two objects are equal according to a rule, you need to override the equals () method in the judged class. The example code is as follows:
If you want to compare the size of two objects (which is also a common interview question), there are two ways:
The compared class implements the comparable interface and rewrites the CompareTo () method to define the class that implements a comparator interface, or rewrites the compare () method by using the internal class. The difference between the former is defined on the compared class and the latter is defined outside the compared class. Through this difference, the advantages and disadvantages of the two are also obvious. The former is simple, but needs to modify the compared class, while the latter does not need to modify the original code and is more flexible.
In the first method, the example code is as follows:
In the second method, the example code is as follows:
The above is the related content of comparison problems in Java. You can discuss other problems in the message area below. Thank you for your support.