Introduction of Immutable Class
Immutable class: The so-called immutable class means that once the instance of this class has been created, it cannot change its member variable values. Many immutable classes, such as Interger, long, and string, are
Transfer from http://developer.51cto.com/art/201503/468905.htmThis article mainly introduces the immutable objects in Java, and the immutability of the string class in Java, so why is the Java String class immutable? Let's analyze it together.Answer
Answer one:
One of the most popular Java face questions is: What are immutable objects (immutable object), what are the benefits of immutable objects, what should be used, or more specifically, why should Java string classes be set to immutable
Immutable Object Design modeApplicable scenarios:1. The state of the modeled object changes infrequently: Set a dedicated thread to create a new immutable object when the state of the modeled object changes. Other threads simply read the state of
This content is I reprint from everywhere to collate, is my study notes, if involves the copyright question, please give me the message.
Or the content of the wrong place, also please great God can help me to point out changes, thank you.
One, the
What is an immutable object? Immutable objects mean that after an object is created, it cannot change its state, and the object is immutable. The inability to change the state means that the member variable within the object cannot be changed, that
String is a class or can represent a string data typeString: Is an object that is not the original type. As an immutable object, you cannot modify its value once it is created. The modification of a string object that already exists is to recreate a
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 =
Java saves a string pool at run time, which makes string a special class.The benefits of the non-variability of the string class(1) A string pool is possible only if the string is immutable. The implementation of a string pool can save a lot of heap
It OPS masters are the dreams that everyone pursues, and their keen sense of smell always seems to be the root cause of the computational system failure. This ability to quickly respond and pinpoint is the result of years of accumulated experience
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.