String对象

概述

背景: 字符串String是程序中使用最多的一种数据,创建字符串对象和其他普通对象一样户占用计算机的资源(时间和空间),所以大量频繁创建字符串对象,会极大程度地影响程序性能。

针对字符串常量,JVM为了提高性能和减少内存开销,为实例化字符串常量时进行了一些优化
(1)为字符串开辟了一个字符串常量池,类似于缓存区
(2)创建字符串常量时,首先会检查字符串常量池中是否存在该字符串,如果存在该字符串,则返回该实例的引用,如果不存在,则实例化创建该字符串,并放入池中。

案例

(1)使用双引号的形式,双引号里面的内容会存在内存中的常量池中

String str1 = "hello";

(2)创建String对象,hello存放在内存的堆中

String str2 = new String("hello");

(3)String中对equals方法进行了重写,比较的是字符串中每一个字符是否相等

String str1 = "hello";
String str2 = new String("hello");
System.out.println(str1.equals(str2)); //true

(4)==号比较两个String类型,比较的是存放地址

String str1 = "hello";
String str2 = new String("hello");
System.out.println(str1 == str2)); //false
//str1指向的是常量池中的hello对象
//str2指向的是堆区中新创建的hello对象

对应内存图

java 字符串直接赋值不会触发new java中字符串作为对象出现_字符串


(5)使用+拼接的字符串也会利用字符串常量池

String s1 = "a";
String s2 = "b";
//使用+拼接
String s3 = "a" + "b"; 
String s4 = s1 + s2;

//s3指向的是常量池中的“ab”对象,变量参与拼接字符串就不会使用常量池
//s4指定的是堆区新建的“ab”对象
System.out.println(s3.equals(s4)); //true,equals方法比较的是每一个字符

System.out.println(s3 == s4); //false,==比较的是地址

(6)被final修饰的String字符串存放在常量池中,拼接后也是放在常量池中

final String s1 = "a";
final String s2 = "b";
//+拼接
String s3 = "a" + "b";
String s4 = s1 + s2;

//s4是由s1和s2使用+号连接得到的,而s1和s2都是final修饰的【常量】
//常量是固定不会变的,在编译期间就能计算出s4的值
//s3指向常量池的“ab”对象,s4指向常量池的“ab”对象

System.out.println(s3.equals(s4)); //true
System.out.println(s3 == s4); //true

(7)intern方法可以在JVM在运行期间,强行使用字符串常量池

String s1 = "a";
String s2 = "b";
String s3 = "a"+"b";
String s4 = (s1+s2).intern();
System.out.println(s3.equals(s4));//true
System.out.println(s3 == s4);//true