1, Integer
Public Static void Main (String[]args) { integer i=new Integer (1); System.out.println (I.intvalue ()); I=2; System.out.println (I.intvalue ());}
We know that the property of integer value is of type: final int value;
And the method Intvalue is to return value.
Obviously, the above two printing results are different, and all are the same reference variable i. Then we modified the integer instance of 1 to change its value to 2?
Obviously, this conclusion is incorrect, because it violates the properties of the final modified variable.
1 Public classTest {2 Public Static voidMain (String[]args) {3Integer i=NewInteger (1); 4 System.out.println (System.identityhashcode (i));5i=2;6 System.out.println (System.identityhashcode (i));7 }8}
With this code, it is known that an instance of value 1 and an instance of value 2 are two different instances. As stated above, because the Value property is final decorated, the class is immutable.
The memory space interpretation diagram is as follows:
It is known from the interpretation diagram that the value is changed, in fact, the new life becomes a space, changing the direction of the reference variable, and the original instance of value 1 becomes a free instance, and becomes the object target of the Java Virtual Machine recycling. Because, to change value, you have to restart a space, which is more consumable, so integer caches the instance of the value of the [-128,127] range class.
2, String
Its property type is final char value[];
Therefore, the string class is also immutable.
As with an integer, changing the value values creates a new space, which is also a performance drain, so Java gives the string literal a cache, if the literal of a string can
Found in the cache pool is then reused (the compilation phase is completed).
1 Public Static voidMain (String[]args) {2String str1= "Feijishuo";3String str2= "Feijishuo";4System.out.println (STR1==STR2);//true5 6String str3= "Feiji";7String str4= "Fei";8String str5= "Ji";9String str6= "Fei" + "Ji";TenString str7=str4+STR5; OneSystem.out.println (STR3==STR6);//true ASystem.out.println (STR3==STR7);//false
13
Final String str8= "Fei";
String str9=str8+ "Ji";
System.out.println (STR3==STR9); True
-}
Code Analysis: STR1 and str2 point to the same instance, because literally, they exist in the cache pool (the compile-time pool).
STR3 and STR6 point to the same instance, because the literal "Fei" and "Ji" are determined at compile time, so STR6 can be determined at compile time, and the literal is already in
There is a cache pool (compile-time pool), so there is no need to generate a new instance, just point the reference variable to the existing instance.
STR3 and STR7 are not the same instance, because a variable can only be known by its indicated value during run time.
STR3 and STR9 are the same instance, because the variable str8 is defined as the final type, so it can be determined during compilation. In fact, the example already exists Chang (compile constant pool)
exists, so no new instances are generated. Thus, the formula that makes up STR9 can be found in a constant pool during compilation, so STR9 is determined during compilation, and its instances already exist in the constant pool.
Immutable classes for Java