Java 中的 intern() 方法有什么作用?
String 方法的 intern() 方法为字符串对象返回一个规范表示形式。由 String 类私下维护一个最初为空的字符串池。
对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为真时,s.intern() == t.intern() 为真。
所有常量字符串和字符串值常量表达式都为内部字符串。
示例
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