String 被声明为 final,因此它不可被继承。
在 Java 8 及之前,内部使用 char 数组存储数据。
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
在 Java 9 及之后,String 类的实现改用 byte 数组存储字符串,同时使用 coder 来标识使用了哪种字符集编码。
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final byte[] value;
/** The identifier of the encoding used to encode the bytes in {@code value}. */
private final byte coder;
}
/**
* final声明的 value数组不能修改它的引用,所以在构造函数中一定要初始化value属性
*/
public String() {
this.value = "".value;
}
/**
* 除非你明确需要 这个original字符串的 副本
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
/**
* 构造一个由byte[]生产的字符串,使用系统默认字符集编码
* 新数组的长度 不一定等于 数组的length
* 如果默认字符集编码不可用时,此构造器无效。
*/
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
/**
* 使用 Unicode编码的int数组 初始化字符串
* 入参数组修改不影响新创建的String
* @since 1.5
*/
public String(int[] codePoints, int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
//count = 0
if (offset <= codePoints.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
final int end = offset + count;
// Pass 1: Compute precise size of char[]
int n = count;
for (int i = offset; i < end; i++) {
int c = codePoints[i];
// 从 U+0000 至 U+FFFF 之间的字符集有时候被称为基本多语言面
// 可以使用单个char来表示这样的代码点
if (Character.isBmpCodePoint(c))
continue;
// 确认c 是不是
else if (Character.isValidCodePoint(c))
n++;
else throw new IllegalArgumentException(Integer.toString(c));
}
// Pass 2: Allocate and fill in char[]
// 得到可以转成有效字符的 个数
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i++, j++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
Character.toSurrogates(c, v, j++);
}
this.value = v;
}
}
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
方法列表:
boolean isEmpty() //当且仅当 length() 为 0 时返回 true
int length() //返回此字符串的长度
boolean contains(CharSequence s) //当且仅当此字符串包含指定的 char 值序列时,返回 true
char charAt(int index) //返回指定索引处的 char 值
String concat(String str) //将指定字符串连接到此字符串的结尾
int indexOf(int ch) //返回指定字符在此字符串中第一次出现处的索引
int lastIndexOf(int ch) //返回指定字符在此字符串中最后一次出现处的索引
String substring(int beginIndex, int endIndex) //返回一个新字符串,它是此字符串的一个子字符串
CharSequence subSequence(int beginIndex, int endIndex) //返回一个新的字符序列,它是此序列的一个子序列
int compareTo(String anotherString) //按字典顺序比较两个字符串
int compareToIgnoreCase(String str) //按字典顺序比较两个字符串,不考虑大小写
boolean equalsIgnoreCase(String anotherString) //将此 String 与另一个 String 比较,不考虑大小写
static String valueOf(double d)
static String valueOf(boolean b)
byte[] getBytes(Charset charset) //使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组
byte[] getBytes(String charsetName) //使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
String toLowerCase(Locale locale) //使用给定 Locale 的规则将此 String 中的所有字符都转换为小写
String toUpperCase(Locale locale)
boolean matches(String regex) //告知此字符串是否匹配给定的正则表达式
String[] split(String regex, int limit) //根据匹配给定的正则表达式来拆分此字符串
boolean startsWith(String prefix, int toffset) //测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean endsWith(String suffix)
static String copyValueOf(char[] data)//返回指定数组中表示该字符序列的
char[] toCharArray() //将此字符串转换为一个新的字符数组
String replace(char oldChar, char newChar) //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
String replaceAll(String regex, String replacement) //使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串
String intern() //返回字符串对象的规范化表示形式,字符串pool中的存在返回,不存在存入pool并返回
String trim()//返回字符串的副本,忽略前导空白和尾部空白
static String format(Locale l, String format, Object... args) //使用指定的语言环境、格式字符串和参数返回一个格式化字符串
以下两点保证String的 不可修改 特点
从内存,同步和数据结构角度分析:
private int hash;//this is used to cache hash code. Facilitating the Use of Other Objects:确保第三方使用。举一个例子:
//假设String.class 有属性 value;
//set的本意是保证元素不重复出现,如果String是可变的,则会破坏这个规则
HashSet<String> set = new HashSet<String>();
set.add(new String("a"));
set.add(new String("b"));
set.add(new String("c"));
for(String a: set)
a.value = "a"; Security:String被广泛用作许多java类的参数,例如 网络连接,打开文件等。字符串不是不可变的,连接或文件将被更改,这可能会导致严重的安全威胁。 该方法认为它连接到一台机器,但事实并非如此。 可变字符串也可能在Reflection中引起安全问题,因为参数是字符串。例子:
boolean connect(string s){
if (!isSecure(s)) {
throw new SecurityException();
}
//here will cause problem, if s is changed before this by using other references.
causeProblem(s);
} 总之,出于效率和安全原因,String被设计为不可变的。 这也是在一般情况下在一些情况下优选不可变类的原因。
在 JAVA 语言中有8中基本类型和一种比较特殊的类型 String 。这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念。常量池就类似一个JAVA系统级别提供的缓存。8种基本类型的常量池都是系统协调的, String 类型的常量池比较特殊。它的主要使用方法有两种:
String 对象会直接存储在常量池中 String 对象,可以使用 String 提供的 intern 方法。intern 方法会从字符串常量池中查询当前字符串是否存在,若不存在就会将当前字符串放入常量池中 java.lang.OutOfMemoryError: PermGen space 错误的。 它的大体实现结构就是: JAVA 使用 jni 调用c++实现的 StringTable 的 intern 方法, StringTable 的 intern 方法跟Java中的 HashMap 的实现是差不多的, 只是不能自动扩容。默认大小是1009
注意点:
Hashtable String.intern -XX:StringTableSize=99991
// JDK6 中执行: false false
// JDK7 中执行: false true
public static void main(String[] args) {
// 声明的字符创变量 -> 堆
String s = new String("1");
s.intern();
// 声明的字符创常量 -> 堆的 Perm 区
String s2 = "1";
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);
}
// JDK6 中执行: false false
// JDK7 中执行: false false
public static void main(String[] args) {
String s = new String("1");
String s2 = "1";
s.intern();
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
String s4 = "11";
s3.intern();
System.out.println(s3 == s4);
}
jdk6内存分析(注:图中绿色线条代表 string 对象的内容指向。 黑色线条代表地址指向)
String s = new String("1");
String s3 = new String("1") + new String("1");
s.intern();
jdk7内存分析-1
String s3 = new String("1") + new String("1"); ,这句代码中现在生成了2最终个对象,是字符串常量池中的“1” 和 JAVA Heap 中的 s3引用指向的对象。中间还有2个匿名的 new String("1") 我们不去讨论它们。此时s3引用对象内容是”11”,但此时常量池中是没有 “11”对象的。 s3.intern(); 这一句代码,是将 s3中的“11”字符串放入 String 常量池中,因为此时常量池中不存在“11”字符串,因此常规做法是跟 jdk6 图中表示的那样,在常量池中生成一个 “11” 的对象,关键点是 jdk7 中常量池不在 Perm 区域了,这块做了调整。常量池中不需要再存储一份对象了,可以直接存储堆中的引用。这份引用指向 s3 引用的对象。 也就是说引用地址是相同的。 String s4 = "11"; 这句代码中”11”是显示声明的,因此会直接去常量池中创建,创建的时候发现已经有这个对象了,此时也就是指向 s3 引用对象的一个引用。所以 s4 引用就指向和 s3 一样了。因此最后的比较 s3 == s4 是 true。 String s = new String("1"); 第一句代码,生成了2个对象。常量池中的“1” 和 JAVA Heap 中的字符串对象。 s.intern(); 这一句是 s 对象去常量池中寻找后发现 “1” 已经在常量池里了。 String s2 = "1"; 这句代码是生成一个 s2的引用指向常量池中的“1”对象。 结果就是 s 和 s2 的引用地址明显不同。图中画的很清晰。 jdk7内存分析-2
s3.intern(); 的顺序是放在 String s4 = "11"; 后了。这样,首先执行 String s4 = "11"; 声明 s4 的时候常量池中是不存在“11”对象的,执行完毕后,“11“对象是 s4 声明产生的新对象。然后再执行 s3.intern(); 时,常量池中“11”对象已经存在了,因此 s3 和 s4 的引用是不同的。 s.intern(); ,这一句往后放也不会有什么影响了,因为对象池中在执行第一句代码 String s = new String("1"); 的时候已经生成“1”对象了。下边的s2声明都是直接从常量池中取地址引用的。 s 和 s2 的引用地址是不会相等的。 小结-从上述的例子代码可以看出 jdk7 版本对 intern 操作和常量池都做了一定的修。主要包括2点:
String#intern
参考:
https://tech.meituan.com/2014...