Why is a Java string an immutable object? (1)
This article mainly introduces immutable objects in Java and immutable String classes in Java. Why is the Java String class immutable? Let's take a look.
Answer 1:
One of the most popular Java interview questions is: what are immutabl
An immutable object in which the object points to an in-memory value that cannot be changed. when a variable is changed, because the value it refers to cannot be changed, it is equivalent to copy the original value and then change, this will open a new address, the variable point to the new address.A mutable object in which the object points to an in-memory value that can be changed. variables (accurately referred to) after the change, in fact, it ref
Reprint: http://blog.csdn.net/zhangjg_blog/article/details/18319521What is an immutable object? As we all know, in java, the String class is Immutable. So what exactly is an immutable object? It can be said that if an object cannot change its state after it has been created, then the object is Immutable. The inability
Objective
Cause
Deep source
Is that really immutable?
Summarize
ObjectiveThese two days in the Java interview related to some of the issues, it is also very lucky to see the following article.http://blog.csdn.net/zhangjg_blog/article/details/18319521The author of this article has a series of articles about Java in-depth study, it is worth a look, personally feel very good, very rewarding.I was touched by a comment th
mutable type Vs immutable type (variable immutable here, refers to whether that piece of content in memory (value) can be changed. )
mutable type (mutable): list, dictionary
Immutable types (unmutable): Numbers, strings, tuples
In the case of an immutable type, when manipulating the object itself, a new are
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
What is an immutable object?As we all know, in java, the String class is Immutable. So what exactly is an immutable object? It can be said that if an object cannot change its state after it has been created, then the object is Immutable. The inability to change the state means that the member variables within the objec
Ext.: http://blog.csdn.net/zhangjg_blog/article/details/18319521What is an immutable object?As we all know, in Java, the string class is immutable. So what exactly is an immutable object? It can be said that if an object cannot change its state after it has been created, then the object is immutable. The inability to c
1. Variable Objects immutable objects① for Python, the meaning of the variable = object means that the variable is bound to the object as a reference, and the object can be used with the variable name when the binding is complete. when an object does not have any object references, it enters the garbage collection process ( Automatic recycling mechanism). ② for a multi-element data structure, such as list,tuple,dict, its implementation is bound to th
The use of OC base mutable dictionaries with immutable dictionaries 1. Immutable dictionaries1.1 Creating an immutable dictionary //Create a dictionary//Note://1, the number of elements is even//2, each of the two elements is a key-value pair//3, value in front, key in rearNsdictionary *dic = [[Nsdictionary alloc] Initwithobjectsandkeys:@"Huang",@"name",@"
This is an old but still popular problem. The strings in Java are designed to be immutable for a number of reasons. The good answer depends on your memory, synchronization, data structure and so on better understanding, below, I summarize some answers.
Requirements in the 1.String pool
A string pool is a special type of storage area in the method region. When a string is created, if the string already exists in the pool, the reference to the existing
1.4.2 understand the use of immutable code
We have discussed immutability in the previous sections about the benefits of functional styles ). The example we use is an elliptic with a border, but the specific behavior of the Code is not clear. When we rewrite the Code with an immutable object, it becomes easier to understand. In the subsequent sections, we will return to this topic and discuss it in more de
String is an immutable class in Java that cannot be modified once instantiated. Once an instance of an immutable class is created, the value of its member variable cannot be modified, and the immutable class has many advantages. Everyone knows why Java designers want to design it as an immutable class, and let's talk a
1. What is an immutable object?As we all know, in Java, the string class is immutable. So what exactly is an immutable object?It can be said that if an object cannot change its state after it has been created, then the object is immutable. the inability to change the state means that the member variables within the ob
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 difference
1. String is a literal constant, while StringBuffer and StringBuilder are string variables. Character content created by string is immutable, and character content created by StringBuffer and Str
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 the value of the underlying data type cannot be changed, that a variable of the reference type cannot point to anoth
When the state of an object is immutable, it is considered an immutable object, and the use of immutable objects as much as possible is recognized as an effective way to build simple and reliable code. Immutable objects are useful in concurrent programs and cannot be corrupted by thread interference or considered incon
Mutable objects and immutable objects
All objects in Python. There is no so-called value-passing call in Python, all passing is a reference to the object, or it can be considered as a pass-through.In Python, objects are divided into variable (mutable) and immutable (immutable) types, tuples (tuple), numeric (number), string (strings) are
This article is divided into the following sections
Concept
Address issues
As a function parameter
Variable parameters are used in a class
function default Parameters
Differences in the implementation of the class
ConceptThe difference between a mutable object and an immutable object is whether the object itself is mutable.Some of the types built into Python
mutable objects: List dict set
The title is a bit around. For example: Declare an immutable group in the header file, such as-(Nsarray *) images, and then define a variable attribute variable in the anonymous class extension, such as @property (Nonatomic, strong) Nsmutablearray *images; //For Uiimageview. As shown below:@interface Starimageview:uiview-(Nsarray *) images; -(void) Addimagewithname: (NSString *) imageName; @end @interface *images; // For Uiimageview @endNow that t
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.