Characteristics of strings:
- The content of the string can never be changed [Key Point].
- Officially because the content of a string can never be changed, all strings can be shared and used.
- String is equivalent to char[] character array in effect, but the underlying principle is byte[] byte array
//第二点 String str1 = "abc";//存储在堆的字符串常量池中 String str2 = "abc"; //内存当中"abc","abc"对象会被创建出来,而且"abc"对象只会被创建一次,内存当中只有一个"abc"对象被创建。 //此时str1和str2可以共享一个"abc"对象 String str3 = new String("abc"); String str4 = new String("abc");//存储在堆中 //备注:JDK1.7之后的JVM将【运行时常量池】从方法区去移除了,在java堆(heap)中开辟空间用来存储运行时常量池 //JDK1.8开始,取消了java方法区(metho area),取而代之的时原空间(metaspace) //JDK1.8中字符串常量池和运行时常量池逻辑上属于方法区,实际上存储在堆内存当中。
- Common 3+1 ways to create String strings
三种构造方法:
public String();创建一个空白的字符串,不包含任何内容
public String(char[] array);根据字符数组的内容,来创建对应的字符串。
public String(byte[] array);根据字节数组来创建对应的字符串
直接创建
String str= "abc";//右边直接用双引号
//备注:直接写上双引号,系统也会认定为字符串对象。