Change Java string Content __java

Source: Internet
Author: User
Tags reflection
change Java string content 1. Foreword

In the Java development of students, more or less have heard such a word, string is immutable, but you have wondered why. Others think that string is variable, because we often write the following code in actual development, and the output of string is changed.

Code 0

String str = "Hello"

str = "World";

System.out.println (str); Output World

In our entry point, the first answer to this doubt, this clear, the content of this article is very easy to understand.

In code 0 , the output does show that we have changed the content of STR, but string is not immutable. First we have to figure out the internal structure of string, which is a final class with a private final char[] value, and that value is the string storage core (for details, see Java String). And string is a reference type, so immutable is value, not str reference, and look at the diagram to see:

As you can see from the diagram above, we simply change the reference point of STR to not change the contents of the source string, so we often refer to the string invariant as its content, that is, that Char value[]. 2. Change String

Then we go to the body and we know that the string immutable is because of the final char value[inside it, and then we start to change this value[].

We all know that the final modified variable, once assigned, is not allowed to change, but when we modify the array, we can change its array members to see the code:

Code 1

Final int a = 1;

A = 2; Compiler error

final char[] arr = {' h ', ' e ', ' l ', ' l ', ' o '};

char[] temp = {' W ', ' O ', ' r ', ' L ', ' d '};

arr = temp; Compile error

Arr[0] = ' t ';//correct

arr[1] = ' t ';//correct

Why so, because the final modifier is char[] arr, so the arr here is a reference, that is, the reference can not be changed, and char[] member changes, does not affect the ARR, so is correct.

In this case, we should know how to modify the value[in string, but we also face the problem that value[] is private, meaning that it can only be accessed by itself, but we can use the reflection mechanism to break it and force the private member to be modified.

Code 2

String str = "Hello";

Field value = String.class.getDeclaredField ("value");//Get String's Value property

value.setaccessible (True); Set access permissions to True

char[] Valueofstr = (char[]) value.get (str);//Get the Value property on str

valueofstr[0] = ' Y ';//Change First character

System.out.println (str);//Output Yello

In code 2 , we use the reflection mechanism to break the access rules for private variables, forcing the modification of STR's content. 3. Summary

With the internal structure of string, we can also change the contents of a string by reflection mechanism. However, this is only used as an experiment and is not actually recommended because it violates encapsulation.

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.