Java String intern()

Java String intern() is a native method. When the intern() method is invoked on a String object, if the String Pool already has a String with the same value, then the reference of String from the Pool is returned. Otherwise, this string object is added to the pool and the reference is returned.

Java String intern()是本机方法。 当在String对象上调用intern()方法时,如果String Pool已经具有相同值的String,则返回Pool中对String的引用。 否则,此字符串对象将添加到池中,并返回引用。

Java String Intern

Java String Intern

Java String Intern

Java String intern() (Java String intern())

Let’s try to understand intern() method with a simple program.

让我们尝试通过一个简单的程序来理解intern()方法。

package com.journaldev.string;

public class StringIntern {

	public static void main(String args[]) {

		String s1 = new String("abc"); // goes to Heap Memory, like other objects
		String s2 = "abc"; // goes to String Pool
		String s3 = "abc"; // again, goes to String Pool

		// Let's check out above theories by checking references
		System.out.println("s1==s2? " + (s1 == s2)); // should be false
		System.out.println("s2==s3? " + (s2 == s3)); // should be true

		// Let's call intern() method on s1 now
		s1 = s1.intern(); // this should return the String with same value, BUT from String Pool

		// Let's run the test again
		System.out.println("s1==s2? " + (s1 == s2)); // should be true now

	}
}

Output:

输出:

s1==s2? false
s2==s3? true
s1==s2? true

字符串intern()示例说明 (String intern() Example Explanation)

  1. When we are using new operator, the String is created in the heap space. So “s1” object is created in the heap memory with value “abc”.

    当我们使用new运算符时,将在堆空间中创建String。 因此,将在堆存储器中创建值为“ abc”的“ s1”对象。
  2. When we create string literal, it’s created in the string pool. So “s2” and “s3” are referring to string object in the pool having value “abc”.

    当我们创建字符串文字时,它是在字符串池中创建的。 因此,“ s2”和“ s3”是指池中具有值“ abc”的字符串对象。
  3. In the first print statement, s1 and s2 are referring to different objects. Hence s1==s2 is returning false.

    在第一个打印语句中,s1和s2引用不同的对象。 因此s1 == s2返回false
  4. In the second print statement, s2 and s3 are referring to the same object in the pool. Hence s2==s3 is returning true.

    在第二个打印语句中,s2和s3引用池中的同一对象。 因此s2 == s3返回true
  5. Now, when we are calling s1.intern(), JVM checks if there is any string in the pool with value “abc” is present? Since there is a string object in the pool with value “abc”, its reference is returned.

    现在,当我们调用s1.intern()JVM会检查池中是否存在值为“ abc”的字符串? 由于池中有一个字符串对象,其值为“ abc”,因此将返回其引用。
  6. Notice that we are calling s1 = s1.intern(), so the s1 is now referring to the string pool object having value “abc”.

    请注意,我们正在调用s1 = s1.intern() ,因此s1现在引用的值为值“ abc”的字符串池对象。
  7. At this point, all the three string objects are referring to the same object in the string pool. Hence s1==s2 is returning true now.

    此时,所有三个字符串对象都引用字符串池中的同一对象。 因此s1 == s2现在返回true

Please watch the below YouTube video for better clarity about the intern() method.

请观看下面的YouTube视频,以更好地了解intern()方法。

演示地址

GitHub Repository. GitHub存储库中签出更多String示例。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/7929/java-string-intern

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值