Home >Java >Javagetting Started >What are the characteristics of String class in java

What are the characteristics of String class in java

王林
王林forward
2020-08-01 16:46:504574browse

What are the characteristics of String class in java

Features:

(Recommended tutorial: java introductory tutorial)

1. The String class is modified by final. It cannot be inherited.

2. The underlying layer of the String class uses an array structure. Before jdk9, char[] was used, and after jdk9, byte[] was used.

3. Once a String object is created, it cannot be modified. A string constant pool is maintained at the bottom layer to achieve sharing.

Note: Every time an object of the String class is modified, a new object will be generated.

(Video tutorial recommendation: java video tutorial)

Splicing of String objects

String constants in the constant pool Strings in the constant pool Constant: The result is a string constant stored in the constant pool

String c = "a"+"b";
String ab = "ab";
System.err.println(ab==c); //输出true123

Variable constant pool: The result is a string constant stored in the heap

String a = "a";
String c = "a"+"b";
System.err.println(c==a+"b"); //输出false123

If the splicing result calls intern () method, the return value is in the constant pool

String a = "a";
String b = "b";
String c = "a"+"b";
System.err.println(c==a+b); //输出false,证明String类的拼接中只要有一个是变量,结果就存在堆中
String a = "a";
String b = "b";
String c = "a"+"b";
System.err.println(c==(a+b).intern()); //输出true,说明如果拼接结果调用intern()方法,返回值就在常量池中

The above is the detailed content of What are the characteristics of String class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete