Why is a string in Java immutable?

Source: Internet
Author: User
Tags comparable

Reprint: http://blog.csdn.net/zhangjg_blog/article/details/18319521

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 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 other objects, and that the state of the object to which the reference type points cannot be changed.

differentiating objects and objects from referencesfor Java beginners, There is always doubt that a string is an immutable Object. Look at the following code:  
    1. String s = "abcabc";
    2. System.out.println ("s =" + s);
    3. s = "123456";
    4. System.out.println ("s =" + s);

Printing Results Are:s = abcabc
s = 123456 first create a String object s, then the value of S to "abcabc", and then let s the value "123456". As you can see from the printout, the value of S has indeed changed. So how do you say the string object is immutable? In fact, there is a misunderstanding: s is just a reference to a string object, not the object itself. An object is an area of memory in memory, and the more member variables, the greater the amount of space that the memory area Occupies. A reference is just a 4-byte data that contains the address of the object it points to, and the object can be accessed by this address. that is, S is just a reference, it points to a specific object, when s= "123456"; after this code executes, a new object "123456" is created, and the reference s re-points to the object of the heart, the original object "abcabc" still exists in memory, Has not Changed. The memory structure looks like this:  a different point of Java and C + + is that in Java it is not possible to manipulate the object itself directly, all objects are pointed to by a reference, which must be used to access the object itself, including getting the value of the member variable, changing the Object's member variables, invoking the Object's methods, and so On. In C + + there are references, objects and pointers to three things, and these three things can access the Object. In fact, the references in Java are conceptually similar to the pointers in C + +, they are the address values of the stored object in memory, but in java, the reference loses some flexibility, such as a reference in Java cannot be added and reduced as a pointer in C + +.  Why is the string object immutable? to understand the immutability of a string, first look at what member variables are in the string class. In JDK1.6, there are several member variables for string:  
  1. Public Final class String
  2. implements java.io.Serializable, comparable<string>, charsequence
  3. {
  4. /** The value is used for character Storage. * *
  5. Private final Char value[];
  6. /** The offset is the first index of the storage, that is Used. * /
  7. private final int offset;
  8. /** The count is the number of characters in the String. * /
  9. private final int count;
  10. /** Cache The hash code for the string * /
  11. Private int hash; //Default to 0
 in JDK1.7, the string class made some changes, mainly changing the behavior of the substring method when it was executed, which is irrelevant to the topic in this Article. The main member variables of the string class in JDK1.7 are Two:  
  1. Public Final class String
  2. implements java.io.Serializable, comparable<string>, charsequence {
  3. /** The value is used for character Storage. * *
  4. Private final Char value[];
  5. /** Cache The hash code for the string * /
  6. Private int hash; //Default to 0


as can be seen from the above code, in Java the String class is actually the encapsulation of the character array. In JDK6, value is an array of string wrappers, offset is the starting position of string in this value array, and count is the number of characters that the string occupies. In JDK7, there is only one value variable, that is, all characters in value are of the object of String. This change does not affect the discussion in this Article. In addition to this, there is a hash member variable, which is the cache of the hash value of the string object, and this member variable is not related to the discussion in this Article. In java, an array is also an object (you can refer to my previous article in Java for an array of attributes). So value is just a reference, it points to a real array object. Actually executed a string s = "abcabc"; After this code, the actual memory layout should look like This:the three variables of Value,offset and Count are private, and no public methods such as setvalue, setoffset, and SetCount are available to modify the values, so the string cannot be modified outside the string class. This means that once the initialization is not modified, and the three members cannot be accessed outside the string class. In addition, the three variables of Value,offset and Count are final, that is, within the string class, once the three values are initialized, they cannot be changed. So you can think of a string object as Immutable.  so in string, there are some methods that can be called to get the changed Values. These methods include substring, replace, replaceall, tolowercase and so On. For example, the following code:  
    1. String A = "abcabc";
    2. System.out.println ("a =" + a);
    3. A = A.replace (' A ', ' a ');
    4. System.out.println ("a =" + a);

Printing Results Are:A = Abcabc
A = Abcabc then the value of a seems to change, in fact, it is the same misunderstanding. again, A is just a reference, not a real string object, when calling A.replace (' a ', ' a '), a new string object is created inside the method, and the object of the heart is re-assigned to reference A. The source code of the Replace method in string can explain the problem:the Reader can view the other methods by re-creating the new string object inside the method, and returning the new object, the original object will not be Changed. This is why there are return values for methods such as replace, substring,tolowercase, and so On. It is also why the call does not change the value of the object as Follows: 
    1. String ss = "123456";
    2. System.out.println ("ss =" + ss);
    3. Ss.replace (' 1 ', ' 0 ');
    4. System.out.println ("ss =" + ss);

Printing results:ss = 123456
ss = 123456  is the string object really immutable? from the above, it is known that the member variable of the string is private final, which is not changed after Initialization. So in these few members, value is more special because he is a reference variable, not a real object. Value is final decorated, that is, final cannot point to other array objects, can I change the array that value points to? For example, change a character in an array to an underscore "_". At least not in the ordinary code we write ourselves, because we simply cannot access this value reference, nor can we modify the array by this reference. So what is the way to access a private member? yes, with reflection, you can reflect the Value property in a String object and change the structure of the array by the value reference Obtained. Here is the instance code: 
  1. Public static void Testreflection () throws Exception {
  2. //create string "Hello world" and assign to reference s
  3. String s = "Hello world";
  4. System.out.println ("s =" + s); //hello world
  5. //gets the Value field in the String class
  6. Field valuefieldofstring = String.  Class.getdeclaredfield ("value");
  7. //change the access rights of the Value property
  8. Valuefieldofstring.setaccessible (true);
  9. //gets The value of the Value property on the S object
  10. char[] value = (char[]) valuefieldofstring.get (s);
  11. //change the 5th character in the array referenced by value
  12. value[5] = ' _ ';
  13. System.out.println ("s =" + s); //hello_world
  14. }

Printing Results Are:s = Hello world
s = Hello_world in This process, s always refers to the same string object, but before and after the reflection, the string object has changed, that is to say, by reflection, the so-called immutable object can be Modified. But generally we do not do so. This example of reflection can also illustrate a problem: if an object, the state of his group of other objects can be changed, then this object is probably not immutable Object. For example, a car object, which combines a wheel object, although the wheel object is declared as private final, but the internal state of the wheel object can be changed, it is not very good to ensure that the car object is Immutable.

Why is a string in Java immutable?

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.