当前位置:网站首页>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 0
String 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 .
边栏推荐
- Summary of common JDBC exceptions and error solutions
- 二叉排序树
- 51 single chip microcomputer, some registers, some knowledge points
- mysql版本升级+数据迁移
- Jenkins 发布PHP项目代码
- Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
- Apache Doris1.0版本集群搭建、负载均衡与参数调优
- 如何进行流程创新,以最经济的方式提升产品体验?
- Object类常用方法
- C ++ 引用与指针总结
猜你喜欢
解析產品開發失敗的5個根本原因
Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
达梦数据库修改字段信息采坑记
Uniapp -- the use of document collation and push of unipush
Kylin
Kotlin null pointer bug
debezium
Database - mongodb
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
在win10下使用visual studio2015链接mysql数据库
随机推荐
sqlServer2008中float类型数据与datetime类型数据互转
phoenix索引
Kotlin null pointer bug
OpenResty篇01-入门简介和安装配置
树莓派开机发送热点进行远程登录
MySQL InnoDB锁知识点
Jenkins 发布PHP项目代码
二叉排序树
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
php中使用google protobuf协议环境配置
谈一谈生产环境中swoole协程创建数量控制机制
Leetcode-1528- rearrange string - hash table - string
聊聊swoole或者php cli 进程如何热重启
mysql
Using Google protobuf protocol environment configuration in PHP
Go language escape analysis complete record
WordPress
Use Baidu map API to set an overlay (infowindow) in the map to customize the window content
Uniapp -- the use of document collation and push of unipush
两种块级元素居中的方式