Today, I found some good posts when I went to the Forum. After reading the posts, I found that I did not pay attention to some of the knowledge points. So I will repost them here to make a memo.
Jdk1.5 introduces autoboxing and unboxing, which facilitates the call of collection classes and some methods and puzzles beginners. Here we will unveil its secret.
First, we need to clarify some concepts:
1. integer is a class, and an integer is used to declare a variable. It is an object type (or reference type); int is a basic type, and the variable declared with int is not an object type, you cannot call a method on it.
2. When "=" is applied to an object, it compares the value of the Object Reference (or the object address is easier to understand ), the value of the basic type is compared to the value of the basic type.
Next, let's take a look at the Code:
public class Test { public static void main(String[] args) { Integer i1 = 2; int i2 = 2; System.out.println(i1 == i2); }}
There are two confusing problems in this Code. First, assign a basic type of value to the reference of the object, that is, integer I1 = 2; the second is to compare an object type with a basic type. It is reasonable to say that both methods are problematic. In JDK 1.5 (if the JDK version is 1.4 or later, you can use javac-source test. for Java compilation), the first problem will be the "incompatible type" error during compilation, and the second problem will be the "Operator
= Cannot be applied to Java. Lang. integer, Int "errors.
However, for the automatic packing and automatic unpacking introduced by JDK, it is necessary to convert one of the types to another. Is it true that the integer object I1 is converted to the basic int type? Or convert I2 of the basic int type to an integer object? You can use javap-C test to decompile the test. Class file to obtain the answer:
public static void main(java.lang.String[]); Code: 0: iconst_2 1: invokestatic #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: astore_1 5: iconst_2 6: istore_2 7: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream; 10: aload_1 11: invokevirtual #4; //Method java/lang/Integer.intValue:()I 14: iload_2 15: if_icmpne 22 18: iconst_1 19: goto 23 22: iconst_0 23: invokevirtual #5; //Method java/io/PrintStream.println:(Z)V 26: return}
[0-4] indicates the implementation of integer I1 = 2. We found that the compiled bytecode calls integer. the valueof method. Therefore, after integer I1 = 2 is compiled, it is equivalent to integer I1 = integer. valueof (2); [] indicates the implementation of int I2 = 2; [] indicates system. out. println (I1 = I2) implementation, it is also easy to see, which calls integer. intvalue () method. Therefore, this I1 = I2 is a comparison between two different types of variables. during compilation, the compiler converts them to the same type for comparison, converts an object type to a basic type, system. out. println (I1
= I2) is equivalent to system. out. println (i1.intvalue () = I2), as mentioned earlier, "=" compares the value of the basic type when acting on the basic type. The two values are both 2, therefore, the result is true.
Another confusing example is:
public class Test { public static void main(String[] args) { Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2); Integer i3 = 128; Integer i4 = 128; System.out.println(i3 == i4); }}
After running, The I1 = I2 result is true, I3 = I4 result is false? This is a headache for unknown people. As we have mentioned in the previous example, integer I1 = 127 is equivalent to integer I1 = integer after compilation. valueof (127), since it is to call a method to obtain the object, it is necessary to find out the valueof method. Let's take a look at the source code:
public static Integer valueOf(int i) { final int offset = 128; if (i >= -128 && i <= 127) { // must cache return IntegerCache.cache[i + offset]; } return new Integer(i);}
At this point, it should be suddenly realized that integercache caches integer objects between [-128,127]. If the valueof parameter I is in the range, it returns the cached object. Otherwise, a new integer is created. As mentioned above, when "=" acts on an object, it compares the object address. In this example, I1 and I2 are obtained from the cache, and of course they are the same object, i3 and I4 are both obtained through the new integer. Of course they are not the same object.
Similarly, Java. lang. long, Java. lang. short caches Long and Short objects between [-128,127], Java. lang. byte caches all objects, Java. lang. character caches character objects between [2, 0,127. Java caches these objects for performance optimization. Since we already know that these objects are cached, new integer/long /... can use integer/long/short... # valueof method.
This article is transferred from ticmy http://www.ticmy.com /? P = 110