简体   繁体   English

String.intern()到底如何工作

[英]String.intern() how exactly work

I am wondering why in the following code: 我想知道为什么在以下代码中:

   String args0 = args[0];
   String args1 = args[1];
   args0.intern();
   args1.intern();

   if (args0==args1){
      System.out.println("Success");
   } else {
      System.out.println("Failure");
   }

by passing as a line command argument 2 identical strings it gives back failure . 通过将2个相同的字符串作为行命令参数传递,它会failure

Thanks in advance. 提前致谢。

Being String inmutable, you'd need to assign the instance intern() returns in order to make the variables reference the interned instance, so that the == comparison would return true . 由于String不可变的,因此您需要分配实例intern()返回值,以使变量引用被实例化的实例,以便==比较将返回true

String args0 = args[0];
String args1 = args[1];
args0 = args0.intern();
args1 = args1.intern();

if (args0==args1){
   System.out.println("Success");
} else {
   System.out.println("Failure");
}

In your example args0 and args1 are still referencing the original distinct instances in the array. 在您的示例中, args0args1仍在引用数组中的原始不同实例。

You have to do: 你必须做:

if (args0.intern() == args1.intern()){
    System.out.println("Success");
}

Strings are immutable . 字符串是不可变的

.intern() returns a (potentially) different String instance that is in the intern pool. .intern()返回.intern()的(可能) 不同的 String实例。

You're throwing this instance away. 您正在丢掉这个实例。

Java automatically interns String literals. Java自动插入String文字。 This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values. 这意味着在许多情况下,==运算符似乎适用于String,其处理方式与ints或其他原始值相同。

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String() . 由于Interning对于String文字是自动的,因此intern()方法将用于使用new String()构造的new String()

You can refer this example also: 您也可以参考此示例:

public class Test
{
   public static void main(String args[]){
      String Str1 = new String("Welcome to Tutorialspoint.com");
      String Str2 = new String("WELCOME TO SUTORIALSPOINT.COM");

      System.out.print("Canonical representation:" );
      System.out.println(Str1.intern());

      System.out.print("Canonical representation:" );
      System.out.println(Str2.intern());
   }
}

(I assume that args0 and args1 holds Strings with same value) (我假设args0args1持有具有相同值的字符串)

  1. args0.intern() will check if String pool already contains String with same value as String from args0 . args0.intern()将检查字符串池是否已包含与args0中的String具有相同值的String。
  2. If pool contains such String reference to it will be returned, if not new String with same value as args0 will be created, placed in pool reference to it returned. 如果pool包含对它的此类String引用,则将返回,如果不是,则创建与args0值相同的新String,并将其放置在返回的Pool引用中。

after

args0.intern();
args1.intern();

args0 and args1 still hold Strings used in command line so they are different from Strings in pool. args0args1仍保留命令行中使用的字符串,因此它们与池中的字符串不同。

To change it try maybe 要改变它,也许尝试

args0 = args0.intern();
args1 = args1.intern();

now args0 = args0.intern(); 现在args0 = args0.intern(); will place "put" String from args0 in pool update args0 to hold that String. 会将来自args0 “ put”字符串args0池更新args0以保存该字符串。 After args1 = args1.intern(); 之后args1 = args1.intern(); intern will see that pool contains same string (placed earlier with args0.intern() ) so will just return reference to it and store it under args1 . 实习生将看到pool包含相同的字符串(之前与args0.intern()放置在一起),因此将仅返回args0.intern()字符串的引用并将其存储在args1下。

Now if(args0 == args1) should be true because both references hods the same String object. 现在if(args0 == args1)应该为true因为两个引用都将同一个String对象作为对象。

Strings are immutable and its methods always return the altered String : 字符串是不可变的,其方法始终返回更改后的String

 args0 = args0.intern();
 args1 = args1.intern();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM