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,
[work] python read txt Last line of file Conflict statement (conflicting declaration) solve exercises 11-5 Output string at specified position ( Essence 2020 year 6 month 2 Daily update ) TypeScript Function explanation [AndroidO] [RK3399] -- GPIO Drive and control mode ( Essence )2020 year 6 month 26 day C# Class library GUID Help class Fiddler Simulate request sending and modifying response data mybatis-config.xml Set default value configuration ( Essence 2020 year 6 month 3 Daily update ) TypeScript Detailed explanation of Chinese interface be based on RK3399 PWM Drive development