查看“Java String 字符串池”的源代码
←
Java String 字符串池
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
<syntaxhighlight lang="java"> 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)); } } </syntaxhighlight><code>System.identityHashCode</code> 是 Java 里 <code>System</code> 类的一个静态方法,其主要用途是返回对象的哈希码,这个哈希码基于对象的内存地址来生成。 *基于内存地址:<code>System.identityHashCode</code> 返回的哈希码是依据对象在内存中的实际地址计算得出的。这意味着即使两个对象的内容相同,但只要它们是不同的实例(即内存地址不同),那么它们的 <code>System.identityHashCode</code> 返回值就会不同。 *忽略 <code>hashCode</code> 方法重写:通常情况下,Java 中的对象可以重写 <code>hashCode</code> 方法,以根据对象的内容来生成哈希码。不过,<code>System.identityHashCode</code> 不会受对象 <code>hashCode</code> 方法重写的影响,它总是返回基于对象内存地址的哈希码。 当使用双引号直接创建字符串时,例如 <code>String str = "hello";</code>,该字符串会被添加到字符串池中。即使方法调用结束,这个字符串实例依然会留在字符串池中,不会被垃圾回收,因为字符串池里的字符串是全局共享的,会被 JVM 长期保留。 字符串池里字面量在JVM退出之后才会回收。
返回至
Java String 字符串池
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息