当前位置:网站首页>今天说说String相关知识点
今天说说String相关知识点
2022-06-25 22:06:00 【程の编程日记】
String
如何实现以及特性
String类被final修饰,所以String不可被继承,Integer等等的包装类都不可以被继承。
jdk1.8中,String底层使用final修饰的char数组来存储字符串
private final char value[];
jdk1.9之后,底层使用final修饰的byte数组来存储字符串
private final byte[] value;
使用final关键字修饰,说明这个value数组初始化之后就不能引用其他数组,String内部也没有改变value数组的方法,因此String不可变。
不可变的好处
缓存哈希值
例如HashMap中使用String作为key,因为不可变,所以哈希值只需要一次计算,String类中也有相关代码。
/** Cache the hash code for the string */ private int hash; // Default to 0String Pool(字符串常量池)的需要
当一个String对象被创建过了,就会是从String Pool取得的引用,如果String可变的话,String Pool就无法使用了。
线程安全
String,StringBuffer,StringBuilder
可变性
String不可变
StringBuffer和StringBuilder可变
这俩为啥可变,因为都继承自一个AbstractStringBuilder,而这个类中提供了构造方法,基于一个容量可变的数组
/** * Creates an AbstractStringBuilder of the specified capacity. */ AbstractStringBuilder(int capacity) { value = new char[capacity]; }两个小弟都是调用父类构造方法,传入一个默认容量16来作为初始容量
// StringBuilder public StringBuilder(String str) { super(str.length() + 16); append(str); } // StringBuffer public StringBuffer(String str) { super(str.length() + 16); append(str); }append方法,容量可控制
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)); } }
线程安全
String 线程安全
StringBuffer线程安全,内部方法加锁,比如append
@Override public synchronized StringBuffer append(String str) { toStringCache = null; super.append(str); return this; }StringBuilder 线程不安全,因为没有加锁,性能要比StringBuffer稍微好一点
使用场景
- String 操作少量数据
- StringBuffer 多线程,操作大量数据
- StringBuilder 单线程,操作大量数据
StringPool
intern方法
可以使用 String 的 intern() 方法在运行过程将字符串添加到 String Pool 中。
当一个字符串调用 intern() 方法时,如果 String Pool 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Pool 中字符串的引用;否则,就会在 String Pool 中添加一个新的字符串,并返回这个新字符串的引用。

举例说明:
String a = new String("aaa");
String b = new String("aaa");
System.out.println(a == b); // false
// 使用intern取得字符串引用
String c = a.intern();
String d = a.intern();
System.out.println(c == d); // true
// 如果是下面这种创建字符串,会直接放入字符串池中
String e = "aa";
String f = "aa";
System.out.println(e == f); // true
通俗来讲,intern()方法就是获取字符串引用,没有的话就放入池子,返回引用,有的话就直接返回引用。
所以我们进行字符串赋值的时候可以使用intern()来通过引用来节省空间。
边栏推荐
- Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
- 期末复习【机器学习】
- mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
- Understanding of pseudo classes
- CSDN force value
- DPVS-FullNAT模式keepalived篇
- 权限设计=功能权限+数据权限
- (serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process
- 7.常用指令(下)v-on,v-bind,v-model的常见操作
- SVN
猜你喜欢

Line height for small use

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

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

idea Kotlin版本升级

Qtcreator formatting code

Wireshark对IMAP抓包分析

Leetcode-1528- rearrange string - hash table - string

达梦数据库修改字段信息采坑记
![mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动

Uniapp -- the use of document collation and push of unipush
随机推荐
How does excel translate Chinese words into English automatically? This formula teaches you
Hibernate entity class curd, transaction operation summary
213.打家劫舍 II
Bi-sql stored procedure (I)
Graduation trip | recommended 5-day trip to London
音频基础知识以及PCM转WAV
Problems encountered in Doris operation and maintenance
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
Doris 运维中遇到的问题
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
DPVS-FullNAT模式keepalived篇
Analyse des cinq causes profondes de l'échec du développement de produits
IDEA中如何生成get/set方法
idea 查看单元测试覆盖率
期末复习【机器学习】
unsigned与signed之大白话
Spark日志分析
OpenResty篇01-入门简介和安装配置
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
c_ uart_ interface_ Example and offboard modes