Java Foundation---immutable object creation

Source: Internet
Author: User
Tags class definition shallow copy

It can be modified by reflection. public static void Stringreflection () throws Exception {   String s = "Hello World";   System.out.prin TLN ("s =" + s); Hello world  //Gets the value field in the String class   Field Valuefield = String.class.getDeclaredField ("value");   //Change the access rights of the Value property   Valuefield.setaccessible (True);   char[] value = (char[]) Valuefield.get (s);  //Change the 5th character in the array referenced by value   VALUE[5] = ' _ ';   System.out.println ("s =" + s); hello_world} === Why is string an immutable object and what are the advantages? The underlying uses private final char value[]; there is a private final array  1. Saves heap space, which is stored in a string constant pool when the string object is created, and the same string can have different pointers pointing at the same time. Guaranteed uniqueness, multithreading when dealing with the same string without worrying about threading issues because it is immutable.  2. Security considerations. ---Database account password---Network programming port number and IP---class loader load class is passed through a string, if the variable will cause system vulnerability  3. The key to use when HashMap is particularly suitable, because Hashcode does not need to repeat the calculation, Direct cache How does this hashcode === create an immutable class? Description: The member variable is the private final type, initialized with constructor cloning, there is no set method, the Get method is accessed using a clone. ---class definition: The final decorated class, which cannot inherit---member variables: 1. The member variable is set to proprietary private final, initialized by the constructor, 2. The Set method is not set for member variables, and 3. You must use CLO when initializing the variable object.NE is initialized in a way that initializes a new object, otherwise it affects the original object. ---method: 1. How to access mutable objects in immutable objects: for example, if you have a map, how do you get it? The Get method cannot directly return the object itself, but instead returns the object by cloning, so that modifying the original data does not affect the newly created data. 2. When the constructor initializes the object, a new hashmap must be created to copy the constructor's entry map, which is the cloning purpose: for example, I have three properties Int,string,hashmap used to initialize an immutable object. Then I get a reference to the HashMap in the object, get the reference using the Hasmap.clone () method, then this reference points to the new array address, but if there is an object reference inside the HashMap, it will not be copied or pointed to the original object. Is the Clone method of  ===hashmap a deep copy or a shallow copy? K key = E.getkey (); V value = E.getvalue ();p utval (hash (key), key, value, false, evict); This is the internal implementation of the entry clone, and the key-value pair will be created. If it is a reference type, only the reference is copied, so when a new or old reference is modified, it points to the object in the same heap space. The Clone method of Http://blog.csdn.net/wangbiao007/article/details/526250991.HashMap generates new objects, and the new objects have their own separate storage space. 2. Although the Clone method of HashMap generates new objects, the reference types stored by the new HashMap and the original hashmap are the same storage space that is pointed to. This results in the modification of elements in the HashMap, i.e. the modification of elements in an array, which causes both the original and clone objects to change, but the addition or deletion does not affect the other, as this is equivalent to the change made by the array, and the Clone object is reborn as a set of arrays.  

Java Foundation---immutable object creation

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.