当前位置:网站首页>Let's talk about string today
Let's talk about string today
2022-06-25 23:51:00 【Program diary】
String
How to implement and feature
String Class quilt final modification , therefore String Not to be inherited ,Integer And so on can not be inherited .
jdk1.8 in ,String Bottom use final Embellished char Array to store strings
private final char value[];
jdk1.9 after , Bottom use final Embellished byte Array to store strings
private final byte[] value;
Use final Keyword modification , Illustrate this value After the array is initialized, you cannot reference other arrays ,String There's no change inside value Array method , therefore String immutable .
Immutable benefits
Cache hash value
for example HashMap Use in String As key, Because it's immutable , So the hash value only needs to be calculated once ,String There is also relevant code in the class .
/** Cache the hash code for the string */ private int hash; // Default to 0String Pool( String constant pool ) The need for
When one String The object has been created , Will be from String Pool Get a reference to , If String Variable words ,String Pool Can't use .
Thread safety
String,StringBuffer,StringBuilder
variability
String immutable
StringBuffer and StringBuilder variable
Why do these two change , Because they all inherit from one AbstractStringBuilder, This class provides constructors , Based on a variable capacity array
/** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; }Both of them call the parent class constructor , Pass in a default capacity 16 As the initial capacity
// StringBuilder public StringBuilder(String str) { super(str.length() + 16); append(str); } // StringBuffer public StringBuffer(String str) { super(str.length() + 16); append(str); }append Method , Capacity controllable
public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) { value = Arrays.copyOf(value, newCapacity(minimumCapacity)); } }
Thread safety
String Thread safety
StringBuffer Thread safety , Internal method locking , such as append
@Override public synchronized StringBuffer append(String str) { toStringCache = null; super.append(str); return this; }StringBuilder Thread unsafe , Because there is no lock , Performance is better than StringBuffer A little better
Use scenarios
- String Operate on a small amount of data
- StringBuffer Multithreading , Working with a lot of data
- StringBuilder Single thread , Working with a lot of data
StringPool
intern Method
have access to String Of intern() Method to add a string to String Pool in .
When a string is called intern() When the method is used , If String Pool A string already exists that is equal to the value of the string ( Use equals() Method to determine ), So it's going to return String Pool A reference to a string in ; otherwise , Will be in String Pool Adds a new string to , And returns a reference to the new string .

Illustrate with examples :
String a = new String("aaa");
String b = new String("aaa");
System.out.println(a == b); // false
// Use intern Get string reference
String c = a.intern();
String d = a.intern();
System.out.println(c == d); // true
// If it is the following creation string , Will be put directly into the string pool
String e = "aa";
String f = "aa";
System.out.println(e == f); // true
Popular speaking ,intern() The method is to get the string reference , If not, put it in the pool , Return reference , If so, it will directly return the reference .
So we can use when we assign a string intern() To save space by referencing .
边栏推荐
猜你喜欢

js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来

C# IO Stream 流(二)扩展类_封装器

森林的先序和中序遍历

1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking

iomanip头文件在实战中的作用

权限设计=功能权限+数据权限

debezium

How does excel translate Chinese words into English automatically? This formula teaches you

产品经理如何把控产品开发的进度

unsigned与signed之大白话
随机推荐
Can I upload pictures without deploying the server?
Hibernate entity class curd, transaction operation summary
推荐系统设计
Compiling protobuf protocol files using makefile in PHP
Talk about how to hot restart a spoole or PHP cli process
第六章 习题(678)【微机原理】【习题】
debezium
Analyse des cinq causes profondes de l'échec du développement de produits
Leetcode-1528- rearrange string - hash table - string
excel如何实现中文单词自动翻译成英文?这个公式教你了
权限设计=功能权限+数据权限
Record the ideas and precautions for QT to output a small number of pictures to mp4
今天说说String相关知识点
Share a downloaded osgeo4w64 Library Based on qgis3.10
mysql
达梦数据库修改字段信息采坑记
CXF
static关键字详解
DPVS-FullNAT模式管理篇
Format the number. If the number is not enough, fill in 0, for example, 1:0001,25:0025