Throw a question.
Let's look at the following code first, do not look at the answer yourself:
public class autoboxingtest {public static void Main (string[] args) {Integer a = 1; Integer B = 2; Integer C = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; System.out.println (c = = d); System.out.println (E = = f); System.out.println (c = = (A + b)); System.out.println (C.equals (A + b)); System.out.println (g = = (A + b)); System.out.println (G.equals (A + b)); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
Then compare it with the correct answer:
If all the right proof that your knowledge is very strong, there is no need to look down, if you find that you have done wrong local law, then patiently look at it.
First say = = and equals () method = =
- For basic types, the wrapper type of the base type is not included, and the value of the base type is compared.
- For an object, = = Compares the address of two objects in memory.
Equals () method
Equals () is the method in the object:
This is the implementation in object objects, because all classes are inherited from the object class, generally will override this method, the implementation is different, the way of comparison, this method is mainly to achieve the logical equality of objects, and this logic is for you to decide.
Here is the Equals () method in the integer class:
This first determines whether the object being compared is an integer, if it is cast to an integer, then compares their values, and returns false directly if the type is different.
Again, the caching mechanism for integer
Starting with Java1.5, the integer type is a cache object:
The cache object Integercache is a static inner class in integer, and the cache range is -128~127.
After my exploration of the source code, I find that there is not only an integer cache mechanism, but all classes of integer types have similar caching mechanisms.
Class |
Cachaclass |
Range |
Character |
Charactercache |
0~127 |
Byte |
Bytecache |
-128~127 |
Short |
Shortcache |
-128~127 |
Longcache |
Longcache |
-128~127 |
Integer |
Integercache |
-128~127 |
Except the Integer can be changed by the parameter range, the others are not.
This problem can be illustrated by initializing the source code for Charactercache and Integercache.
Automatic boxing and automatic unpacking
When you assign a value to its wrapper class with a base type value, the system automatically converts the base type to its wrapper type.
For example: integer i = 1, when the system assigns 1 to the integer type I. The valueof () method of the time integer used.
The first is to determine whether the value is before low and high, that is, if a cached object already exists with the same value as the current value, and if there is a direct return to the object, the new object is re-existing.
Automatic unpacking
In contrast to automatic boxing, when you need a basic type, the system automatically converts the wrapper class to the base type.
For example:
Integer a1=1;int a2 = a1;//这时候系统就会将Integer类型的a1自动拆箱,把值取出来赋值给a2
This time the system calls the integer Intvalue () method:
is the direct return value.
It's easy to see the problem again.
C==d is true. Both C and D are cached object 3, which is the same object, so they are equal.
E==f is false. Both E and F are beyond the scope of the cached object, are new objects, are different objects, so the address in memory must be different, so it is false.
c = = (A + b), this should be noted that in the case of arithmetic operations, the automatic unpacking operation, that is, the comparison is the value of C and a+b value of the relationship.
At first I thought that the value of A+b is 3, and then this 3 is the cache object, so equal, but I in the debug follow-up, I found that the call 3 times Integer.intvalue (), and then went to check, found in the arithmetic operation, will be automated unpacking operation.
C.equals (A + B) This is the Equals () method that calls the integer, which first automates the unpacking of A and B, then compares them to the auto-boxing operation, and then calls the Equals () method, because the Equals () of the integer The method has been rewritten, as described above, to compare the values of two objects, so it returns true.
G = = (A + b) This first will be like the 4th, the first will be automatically unpacking G, and then A and B automatic unpacking operation, then the comparison of their values, there will also be an implicit type conversion, Int→long. Returns true because the value is the same.
G.equals (A + B) This invokes the Equals () method of long, and the type of a+b is integer, so it returns false.
java = = and Equals () and auto-boxing unpacking