Immutable classes for Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.