Java String 字符串池
Jihongchang(讨论 | 贡献)2025年3月23日 (日) 03:16的版本 (建立内容为“<syntaxhighlight lang="java"> public class StringPoolTest { // 测试使用双引号创建字符串 public static void testStringLiteral() { Stri…”的新页面)
public class StringPoolTest {
// 测试使用双引号创建字符串
public static void testStringLiteral() {
String str1 = "testLiteral";
System.out.println("方法内 str1 的引用: " + System.identityHashCode(str1));
}
// 测试使用 new 关键字创建字符串
public static void testNewString() {
String str2 = new String("testNew");
System.out.println("方法内 str2 的引用: " + System.identityHashCode(str2));
}
// 测试使用 intern() 方法
public static void testIntern() {
String str3 = new String("testIntern").intern();
System.out.println("方法内 str3 的引用: " + System.identityHashCode(str3));
}
public static void main(String[] args) {
// 测试双引号创建字符串
testStringLiteral();
String sameLiteral = "testLiteral";
System.out.println("方法外 sameLiteral 的引用: " + System.identityHashCode(sameLiteral));
// 测试 new 关键字创建字符串
testNewString();
String newStr = new String("testNew");
System.out.println("方法外 newStr 的引用: " + System.identityHashCode(newStr));
// 测试 intern() 方法
testIntern();
String internStr = "testIntern";
System.out.println("方法外 internStr 的引用: " + System.identityHashCode(internStr));
}
}
System.identityHashCode
是 Java 里 System
类的一个静态方法,其主要用途是返回对象的哈希码,这个哈希码基于对象的内存地址来生成。
- 基于内存地址:
System.identityHashCode
返回的哈希码是依据对象在内存中的实际地址计算得出的。这意味着即使两个对象的内容相同,但只要它们是不同的实例(即内存地址不同),那么它们的System.identityHashCode
返回值就会不同。 - 忽略
hashCode
方法重写:通常情况下,Java 中的对象可以重写hashCode
方法,以根据对象的内容来生成哈希码。不过,System.identityHashCode
不会受对象hashCode
方法重写的影响,它总是返回基于对象内存地址的哈希码。