Recently asked why the string was designed to be immutable, then a little confused, the problem has always been like a bug, unexpectedly did not find, did not think, summed up in this.
The 1.String is immutable
The string class is final decorated and is not inheritable or modified . When a string variable is assigned a second time, instead of modifying the data at the original memory address, it re- opens a memory address in memory and points to the new address.
Why is the string class designed to be final?
1. immutability supports thread safety .
2. immutability supports string constant pooling to improve performance .
As one of the most common data types, the 3.String string is immutable to prevent arbitrary modification and ensures the security of the data.
Under normal circumstances, the string string of Java is final and immutable. However, it is possible to modify its contents by special means.
The main member field of the string class value is a char[] array and is final decorated. Final-modified fields cannot be changed after they are created. Because although value is immutable, it is only the reference address of value that is immutable. The fact that array arrays are mutable is not blocked. The data structure of the array looks:
This means that the array variable is just a reference on the stack, and the structure of the array is in the heap. The value in the string class is final decorated, except that the reference address of the stack called value is immutable. Not that the array itself is immutable in the heap.
Code test:
1String test ="Immutable String";2String test1 =test;3String test2 =NewString (test);4String test3 =NewString (Test.tochararray ());5Field values = String.class. Getdeclaredfield ("value");6Values.setaccessible (true);7 Char[] chars = (Char[]) values.Get(test);8chars[0] ='u';9chars[1] ='N';TenSystem. out. println ("Test==test1:"+ (Test = =test1)); OneSystem. out. println ("Test==test2:"+ (Test = =test2)); ASystem. out. println ("Test1==test2:"+ (Test1 = =test2)); -System. out. println ("Test:"+ Test +"test1:"+ Test1 +"test2:"+ Test2 +"test3:"+ test3);
Derived from the immutability of string to other basic data types: Byte,short,integer,long,double,float,character,boolean Eight basic data wrapper class, look at the discovery is final decoration, After a closer look at the enum enum type, it is found that compiling with javac and then using JAVAP to Decompile is also a class that is compiled to final decoration, and that its enumeration values are all defined as member variables of the static final decoration.
It is found that Java designers in the design of Java basic data types, the basic data types are designed to be immutable, so that both developers and ensure the security of the data.
Summary:string in Java is immutable, but its contents can be modified by reflection .
Note:
Shengming Zeng
Blog: http://www.cnblogs.com/zengming/
This article is original, welcome to reprint, but the reprint must indicate the source of the article, and at the beginning of the article is clearly linked to the Ming.
< welcome students with different ideas or opinions to discuss together and progress together >
Why is the string class designed to be final in Java? Is string really immutable? Are other basic types of wrapper classes immutable?