Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is String immutable in Java?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "is String immutable in Java". In daily operation, I believe that many people have doubts about whether String is immutable in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "is String immutable in Java?" Next, please follow the editor to study!

Why String is not changeable

The String class uses an array of char types modified by final to store characters. The source code is as follows:

/ * The value is used for character storage. * / private final char value []; Why String is not changeable

The String class uses an array of char types modified by final to store characters. The source code is as follows:

/ * The value is used for character storage. * / private final char value []; is String really immutable?

1. String is immutable but does not mean that references are immutable

String str= "Hello"; str= str + "World"; System.out.println ("str=" + str); xi

Effect:

Str=Hello World

Parsing: the content of String is immutable, because str changes from the original memory address pointing to "Hello" to the memory address of "Hello World", so it opens up more memory areas to the "Hello World" string.

2. Immutable objects can be modified by reflection

/ / create the string "Hello World" and assign it to the reference sString s = "Hello World"; System.out.println ("s =" + s); / / Hello World// to get the value field Field valueFieldOfString = String.class.getDeclaredField ("value") in the String class; / / change the access permission of the value property valueFieldOfString.setAccessible (true); / / get the value of the value property on the s object char [] value = (char []) valueFieldOfString.get (s) / / change the fifth character value [5] ='_'; System.out.println ("s =" + s); / / Hello_World in the array referenced by value

Effect:

S = Hello Worlds = Hello_World

Parsing:

Use reflection to access private members, reflect the value property of the object, and then change the value reference to change the array structure.

At this point, the study of "is String immutable in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report