be familiar with Java We all know String Class is immutable , but String Why classes are designed to be immutable , How to be immutable without careful consideration .

String Why to design immutable class ?
1. accord with Java The design of string pool .     String s1="abc"; String s2="abc";     
Java Save memory space through the design of string pool , As above
A piece of code will only generate one object to put in the constant pool .s1 and s2 All point to this object , If String Class variable , adopt s1 This reference can modify this object , Then other references will be affected .
2. Security .      
JDK Many API among , Most of the parameters are String type , Such as class loading function , Connection of database ,Sql sentence ,Socket Parameters of . If String Classes can be modified to create a security vulnerability . And in the case of multithreading ,String Class data can also protect data from being modified by other threads .
String How to realize immutability ?
public final class String implements, Comparable<String>,
CharSequence { private final char value[]; from String Class can be seen in the source code ,String Yes char
value[] Save character's . And declared as private final, And it doesn't provide us with writing value Interface of . therefore String Class cannot be modified .

( actually String Class is not immutable , Through java Reflection mechanism of , Get to string Object's class object , hold value attribute private Change to accessible type , Then you can directly access the object value Attribute )

StringBuffer and StringBuilder The difference between
StringBUffer and StringBuilder All variable string classes ,StringBuffer and StringBuilder The difference between classes is that
StringBuffer Is thread safe , There are many ways synchronized keyword , The following code .StringBuilder Not thread safe , So in general
StringBuilder Good performance .
//StringBuffer Source code of class public synchronized void ensureCapacity(int
minimumCapacity) { if (minimumCapacity > value.length) {
expandCapacity(minimumCapacity); } } /** * @since 1.5 */ public synchronized
void trimToSize() { super.trimToSize(); } /** * @throws
IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized
void setLength(int newLength) { super.setLength(newLength); } /** * @throws
IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized
char charAt(int index) { if ((index < 0) || (index >= count)) throw new
StringIndexOutOfBoundsException(index); return value[index]; }

©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library )Unity3D of UGUI Basics -- Three modes of canvas os Simple use of module computer network --- Basic concepts of computer network ( agreement , system )html Writing about cherry trees , Writing about cherry trees Some East 14 Pay change 16 salary , Sincerity or routine ?Unity-Demo Examples ✨ realization UI- Backpack equipment drag function 06【 Interpretation according to the frame 】 Data range filtering -- awesome java Four functional interfaces ( a key , simple )