Java 中 intern() 方法有什么用?
字符串方法 intern() 返回字符串对象的规范表示形式。字符串类私下保存一个最初为空的字符串池。
对于任意两个字符串 s 和 t,如果且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
所有文字字符串和字符串值常量表达式都已作为内部项。
示例
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str1 = "This is TutorialsPoint";
// returns canonical representation for the string object
String str2 = str1.intern();
// prints the string str2
System.out.println(str2);
// check if str1 and str2 are equal or not
System.out.println("Is str1 equal to str2 ? = " + (str1 == str2));
}
}输出
This is TutorialsPoint Is str1 equal to str2 ? = true
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP