One ： What is immutable object ?
java Language requires us to comply with the following 5 Rules
* All fields within the class are final Decorated .
* All fields within the class are private , That is to say private modification .
* Class cannot be integrated and extended .
* Class cannot provide external methods that can modify internal state ,setter No way .
* If the field inside the class is a reference , That is, it can point to mutable objects , Then we programmers can't get this app
Two ：String How to design an immutable object
* Doubt one
We said it at the beginning of the article ,String Object is immutable , here a= Zhang San , then a= Li Si , This is consistent with String The immutability of ? The answer is, of course .
We can see from the picture above , For the first time String
a=" Zhang San " When , The same object was created in the heap “ Zhang San ”. And then we were doing it a=" Li Si " And then an object is created in memory “ Li Si ”. Which means our a Just changed the reference a It's just the address
2. Source code explanation confusion
The two main fields above ：value and hash. We are here mainly to see value array ,hash It's not about the subject, so it's not going to be covered here , I have a special introduction hash.
ours String Objects are just characters inside and stored here value In the array . however value No external setValue Method of , So the whole String Objects look immutable on the outside . Let's draw a picture to explain the above doubts
Now I see , That is to say, what really changes the quotation is value, because value Also an array reference . It's easy to explain the next question .
3, Doubt two
Since our String Is immutable , There seems to be a lot more inside substring, replace,
replaceAll Methods of these operations . It's all right String Object changed , It's easy to explain , Every time we do replace These operations , In fact, a new object is created in the heap memory . And then our value Point to different objects .
During the interview, we just explained that the above reasons are not so perfect , Want to get a better raise and pretend to be forced , We need further answers .
Three , What can be changed String
Now that it's titled . There must be a way , Don't forget our reflex mechanism , In general , He can do things that violate the principles of language design . It's also a technique , Every time an interviewer asks questions that violate language design principles , You can counter him with a reflex . Let's take a look ：
We can change it by reflection String.
Now we know its principle and usage , We also know that it can be changed by reflection String, There's another problem we haven't figured out , You can ask him back during the interview , In order to further improve their own situation .
Four ,JAVA Why does language put String Type is designed to be immutable
Here are a few features .
first ： stay Java In process String Type is the most used , This involves a lot of additions, deletions, modifications and searches , Every time before adding, deleting, or correcting jvm Need to check this String Security of objects , Is through hashcode, When designed as immutable , It ensures that every time you add, delete, modify or search hashcode Uniqueness of , It can be operated safely .
second ： Network connection address URL, File path path In general, it is String Type save ,
If String Not fixed , All kinds of potential safety hazards will be caused . It's like our password can't String Type save for ,, If you save the password as a string in clear text , Then it will stay in memory , Until the garbage collector clears it . Because strings are placed in the string buffer pool for reuse , So it could be in memory for a long time , And this will lead to security risks
third ： String values are reserved in the constant pool , That is, if the string object allows changes , It will lead to various logical errors
OK, That's all String Why type objects are immutable