Two ways to create String and constant pool in Java

Two ways to create String

1. Constant creation:

String str1 = "abcd";

When we create a string constant, the JVM will first check the string constant pool. If the string already exists in the constant pool, it will directly return the instance reference in the constant pool. If the string does not exist in the constant pool, the string will be instantiated and placed in the constant pool. Due to the immutability of String strings, we can be quite sure that there must not be two identical strings in the constant pool.

The above line of code can be understood as: check the string constant pool there "abcd", the string constant pool if not, create one, then str1pointing to the object string constant pool. If so, then directly to the str1point "abcd";

2. Object creation:

String str2 = new String("abcd");//堆中创建一个新的对象
String str3 = new String("abcd");//堆中创建一个新的对象

This form of creation is the same as the form of creating an object. The created object is stored in the heap and points to the stack. Therefore, str2and str3memory address in the stack, the stack point “abcd”, and the str1memory address points to a string constant pool.

These two different creation methods are different:

  1. The first way is to get objects in the constant pool;
  2. The second way is to create a new object directly in the heap memory space.
System.out.println(str1==str2);//false
System.out.println(str2==str3);//false

Remember one thing: as long as you use it new, you need to create a new object.

Another picture should be easier to sort out (picture source: https://www.journaldev.com/797/what-is-java-string-pool )

Insert picture description here

 
 

String constant pool

The constant pool of String type is special. There are two main ways to use it:

  1. String objects declared directly using double quotation marks will be directly stored in the constant pool.
  2. String object if it is not declared in double quotation marks, String can be used to provide the internmethod.

Supplement: String.intern()

String.intern()Is a Native method. Its role is:

  1. If the runtime constant pool already contains a string equal to the content of this String object, a reference to the string in the constant pool will be returned;
  2. If not, the processing method before JDK1.7 (not including 1.7) is to create a string with the same content as this String in the constant pool and return a reference to the string created in the constant pool. The processing method for JDK1.7 and later is to record the reference of this string in the constant pool and return the reference.
String s1 = new String("计算机");
String s2 = s1.intern();
String s3 = "计算机";
System.out.println(s2);//计算机
System.out.println(s1 == s2);//false,因为一个是堆内存中的 String 对象一个是常量池中的 String 对象,
System.out.println(s3 == s2);//true,因为两个都是常量池中的 String 对象

 

Add a little knowledge point: the splicing of strings:

String str1 = "str";
String str2 = "ing";
		 
String str3 = "str" + "ing";//常量池中的对象
String str4 = str1 + str2; //在堆上创建的新的对象	  
String str5 = "string";//常量池中的对象

System.out.println(str3 == str4);//false
System.out.println(str3 == str5);//true
System.out.println(str4 == str5);//false

Try to avoid concatenation of multiple strings, as this will recreate the object. If you need to change the string, you can use StringBuilder or StringBuffer.
 
 
ps: If you want to learn about 8 basic types of packaging classes and constant pools, you can refer to another blog of mine:
https://blog.csdn.net/weixin_43901865/article/details/112566955

Guess you like

Origin blog.csdn.net/weixin_43901865/article/details/112782233