我们什么时候应该对字符串文字使用 String 的 intern 方法

发布于 2024-08-13 20:37:19 字数 687 浏览 2 评论 0原文

根据 String#intern(), intern 方法如果在字符串池中找到了该字符串,则返回字符串池中的字符串,否则会在字符串池中添加一个新的字符串对象,并返回该字符串的引用。

所以我尝试了这个:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
}

我期望在 s3 被实习时打印 s1 and s3 are same ,并且 s1 and s2 are same 将不会被打印。但结果是:两行都被打印出来。这意味着,默认情况下,字符串常量是被保留的。但如果是这样,那为什么我们需要 intern 方法呢?换句话说,我们什么时候应该使用这个方法呢?

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.

So i tried this:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
}

I was expecting that s1 and s3 are same will be printed as s3 is interned, and s1 and s2 are same will not be printed. But the result is: both lines are printed. So that means, by default String constants are interned. But if it is so, then why do we need the intern method? In other words when should we use this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

家住魔仙堡 2024-08-20 20:37:19

Java 自动实习字符串文字。这意味着在许多情况下,== 运算符对字符串的作用与对整数或其他原始值的作用相同。

由于字符串文字的实习是自动的,因此 intern() 方法将用于使用 new String() 构造的字符串

使用您的示例:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
String s4 = new String("Rakesh");
String s5 = new String("Rakesh").intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
}

if ( s1 == s4 ){
    System.out.println("s1 and s4 are same" );  // 3.
}

if ( s1 == s5 ){
    System.out.println("s1 and s5 are same" );  // 4.
}

将返回:

s1 and s2 are same
s1 and s3 are same
s1 and s5 are same

在所有情况下除了 s4 变量(其值是使用 new 运算符显式创建的,并且在其结果上未使用 intern 方法)之外,它是一个返回的单个不可变实例 JVM 的字符串常量池

有关详细信息,请参阅 JavaTechniques“字符串相等和实习”

Java automatically interns String literals. 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.

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()

Using your example:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
String s4 = new String("Rakesh");
String s5 = new String("Rakesh").intern();

if ( s1 == s2 ){
    System.out.println("s1 and s2 are same");  // 1.
}

if ( s1 == s3 ){
    System.out.println("s1 and s3 are same" );  // 2.
}

if ( s1 == s4 ){
    System.out.println("s1 and s4 are same" );  // 3.
}

if ( s1 == s5 ){
    System.out.println("s1 and s5 are same" );  // 4.
}

will return:

s1 and s2 are same
s1 and s3 are same
s1 and s5 are same

In all the cases besides of s4 variable, a value for which was explicitly created using new operator and where intern method was not used on it's result, it is a single immutable instance that's being returned JVM's string constant pool.

Refer to JavaTechniques "String Equality and Interning" for more information.

秋意浓 2024-08-20 20:37:19

在最近的一个项目中,一些巨大的数据结构是用从数据库读取的数据(因此不是字符串常量/文字)设置的,但有大量的重复。这是一个银行应用程序,到处都出现了诸如一些规模不大(可能有 100 或 200 家)公司的名称之类的东西。数据结构已经很大,如果所有这些公司名称都是唯一的对象,它们就会溢出内存。相反,所有数据结构都引用相同的 100 或 200 个 String 对象,从而节省了大量空间。

驻留字符串的另一个小优点是,如果保证所有涉及的字符串都被驻留,则可以使用 == (成功!)来比较字符串。除了更精简的语法之外,这也是性能的增强。 但是正如其他人所指出的,这样做存在引入编程错误的巨大风险,因此这只能作为最后的手段。

缺点是,内部字符串比简单地把它扔到堆上需要更多的时间,并且内部字符串的空间可能是有限的,具体取决于 Java 实现。当您处理已知合理数量且具有许多重复项的字符串时,最好这样做。

On a recent project, some huge data structures were set up with data that was read in from a database (and hence not String constants/literals) but with a huge amount of duplication. It was a banking application, and things like the names of a modest set (maybe 100 or 200) corporations appeared all over the place. The data structures were already large, and if all those corp names had been unique objects they would have overflowed memory. Instead, all the data structures had references to the same 100 or 200 String objects, thus saving lots of space.

Another small advantage of interned Strings is that == can be used (successfully!) to compare Strings if all involved strings are guaranteed to be interned. Apart from the leaner syntax, this is also a performance enhancement. But as others have pointed out, doing this harbors a great risk of introducing programming errors, so this should be done only as a desparate measure of last resort.

The downside is that interning a String takes more time than simply throwing it on the heap, and that the space for interned Strings may be limited, depending on the Java implementation. It's best done when you're dealing with a known reasonable number of Strings with many duplications.

给我一枪 2024-08-20 20:37:19

更喜欢 String.equals 而不是 this==object

我想在使用 == 和 interned 字符串时添加我的 2 美分。

String.equals 做的第一件事是 this==object。请参阅 OpenJDK 上的源代码

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    …

因此,尽管有一些微小的性能提升(您没有调用方法),但从维护者的角度来看,使用 == 是一场噩梦,因为一些 interned 字符串有变成非 interned 的趋势。

因此,我建议不要依赖 == 的特殊情况来存储字符串,而应始终按照 Gosling 的意图使用 equals

编辑:实习生变成非实习生:

V1.0
public class MyClass
{
  private String reference_val;

  ...

  private boolean hasReferenceVal ( final String[] strings )
  {
    for ( String s : strings )
    {
      if ( s == reference_val )
      {
        return true;
      }
    }

    return false;
  }

  private void makeCall ( )
  {
     final String[] interned_strings =  { ... init with interned values ... };

     if ( hasReference( interned_strings ) )
     {
        ...
     }
  }
}

在 2.0 版本中,维护者决定将 hasReferenceVal 公开,但没有详细说明它需要实习字符串数组。

V2.0
public class MyClass
{
  private String reference_val;

  ...

  public boolean hasReferenceVal ( final String[] strings )
  {
    for ( String s : strings )
    {
      if ( s == reference_val )
      {
        return true;
      }
    }

    return false;
  }

  private void makeCall ( )
  {
     final String[] interned_strings =  { ... init with interned values ... };

     if ( hasReference( interned_strings ) )
     {
        ...
     }
  }
}

现在你有一个错误,可能很难找到,因为在大多数情况下数组包含文字值,有时使用非文字字符串。如果使用 equals 而不是 ==,则 hasReferenceVal 仍将继续工作。再次强调,性能提升微乎其微,但维护成本却很高。

Prefer String.equals over this==object

I want to add my 2 cents on using == with interned strings.

The first thing String.equals does is this==object. See source code on OpenJDK.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    …

So although there is some miniscule performance gain ( you are not calling a method), from the maintainer point of view using == is a nightmare, because some interned strings have a tendency to become non-interned.

So I suggest not to rely on special case of == for interned strings, but always use equals as Gosling intended.

EDIT: interned becoming non-interned:

V1.0
public class MyClass
{
  private String reference_val;

  ...

  private boolean hasReferenceVal ( final String[] strings )
  {
    for ( String s : strings )
    {
      if ( s == reference_val )
      {
        return true;
      }
    }

    return false;
  }

  private void makeCall ( )
  {
     final String[] interned_strings =  { ... init with interned values ... };

     if ( hasReference( interned_strings ) )
     {
        ...
     }
  }
}

In version 2.0 maintainer decided to make hasReferenceVal public, without going into much detail that it expects an array of interned strings.

V2.0
public class MyClass
{
  private String reference_val;

  ...

  public boolean hasReferenceVal ( final String[] strings )
  {
    for ( String s : strings )
    {
      if ( s == reference_val )
      {
        return true;
      }
    }

    return false;
  }

  private void makeCall ( )
  {
     final String[] interned_strings =  { ... init with interned values ... };

     if ( hasReference( interned_strings ) )
     {
        ...
     }
  }
}

Now you have a bug, that may be very hard to find, because in majority of cases array contains literal values, and sometimes a non-literal string is used. If equals were used instead of == then hasReferenceVal would have still continue to work. Once again, performance gain is miniscule, but maintenance cost is high.

捂风挽笑 2024-08-20 20:37:19

学习 Java String Intern - 一劳永逸

Java 中的字符串在设计上是不可变的对象。因此,即使两个字符串对象具有相同的值,默认情况下也会是不同的对象。然而,如果我们希望节省内存,我们可以通过称为字符串实习生的概念来指示使用相同的内存。

以下规则将帮助您清楚地理解这个概念:

  1. String 类维护一个最初为空的内部池。该池必须保证包含仅具有唯一值的字符串对象。
  2. 所有具有相同值的字符串文字必须被视为相同的内存位置对象,因为否则它们没有区别的概念。因此,具有相同值的所有此类文字将在内部池中形成单个条目,并且将引用相同的内存位置。
  3. 两个或多个文字的串联也是文字。 (因此规则#2将适用于它们)
  4. 作为对象创建的每个字符串(即通过除文字之外的任何其他方法)将具有不同的内存位置,并且不会在内部池中创建任何条目
  5. 文字与非文字的串联将做一个非字面的。因此,生成的对象将具有新的内存位置,并且不会在内部池中创建条目。
  6. 在字符串对象上调用 intern 方法,要么创建一个进入 intern 池的新对象,要么从池中返回具有相同值的现有对象。对不在实习池中的任何对象的调用不会将对象移动到池中。它而是创建另一个进入池的对象。

示例:

String s1=new String ("abc");
String s2=new String ("abc");
If (s1==s2)  //would return false  by rule #4
If ("abc" == "a"+"bc" )  //would return true by rules #2 and #3
If ("abc" == s1 )  //would return false  by rules #1,2 and #4
If ("abc" == s1.intern() )  //would return true  by rules #1,2,4 and #6
If ( s1 == s2.intern() )      //wound return false by rules #1,4, and #6

注意:这里不讨论字符串实习生的动机案例。然而,节省内存肯定是主要目标之一。

Learn Java String Intern - once for all

Strings in java are immutable objects by design. Therefore, two string objects even with same value will be different objects by default. However, if we wish to save memory, we could indicate to use same memory by a concept called string intern.

The below rules would help you understand the concept in clear terms:

  1. String class maintains an intern-pool which is initially empty. This pool must guarantee to contain string objects with only unique values.
  2. All string literals having same value must be considered same memory-location object because they have otherwise no notion of distinction. Therefore, all such literals with same value will make a single entry in the intern-pool and will refer to same memory location.
  3. Concatenation of two or more literals is also a literal. (Therefore rule #2 will be applicable for them)
  4. Each string created as object (i.e. by any other method except as literal) will have different memory locations and will not make any entry in the intern-pool
  5. Concatenation of literals with non-literals will make a non-literal. Thus, the resultant object will have a new memory location and will NOT make an entry in the intern-pool.
  6. Invoking intern method on a string object, either creates a new object that enters the intern-pool or return an existing object from the pool that has same value. The invocation on any object which is not in the intern-pool, does NOT move the object to the pool. It rather creates another object that enters the pool.

Example:

String s1=new String ("abc");
String s2=new String ("abc");
If (s1==s2)  //would return false  by rule #4
If ("abc" == "a"+"bc" )  //would return true by rules #2 and #3
If ("abc" == s1 )  //would return false  by rules #1,2 and #4
If ("abc" == s1.intern() )  //would return true  by rules #1,2,4 and #6
If ( s1 == s2.intern() )      //wound return false by rules #1,4, and #6

Note: The motivational cases for string intern are not discussed here. However, saving of memory will definitely be one of the primary objectives.

别理我 2024-08-20 20:37:19

默认情况下,字符串文字和常量会被保留。
也就是说,"foo" == "foo"(由字符串文字声明),但是 new String("foo") != new String("foo")

String literals and constants are interned by default.
That is, "foo" == "foo" (declared by the String literals), but new String("foo") != new String("foo").

倥絔 2024-08-20 20:37:19

你应该弄清楚两个时间段,分别是编译时和运行时。例如:

//example 1 
"test" == "test" // --> true 
"test" == "te" + "st" // --> true

//example 2 
"test" == "!test".substring(1) // --> false
"test" == "!test".substring(1).intern() // --> true

一方面,在示例1中,我们发现结果都返回true,因为在编译时,jvm会放入“test” "到文字字符串池中,如果jvm发现“test”存在,那么它将使用存在的字符串,在示例1中,“test”字符串都指向相同的内存地址,因此示例1将返回true 。
另一方面,在示例2中,substring()方法在运行时执行,
在 "test" == "!test".substring(1) 的情况下,池将创建两个字符串对象,"test"
和"!test",所以它们是不同的引用对象,所以这种情况会返回false,在"test" == "!test".substring(1).intern()的情况下,intern()方法会将 ""!test".substring(1)" 放入文字字符串池中,因此在这种情况下,它们是相同的引用对象,因此将返回 true。

you should make out two period time which are compile time and runtime time.for example:

//example 1 
"test" == "test" // --> true 
"test" == "te" + "st" // --> true

//example 2 
"test" == "!test".substring(1) // --> false
"test" == "!test".substring(1).intern() // --> true

in the one hand,in the example 1,we find the results are all return true,because in the compile time,the jvm will put the "test" to the pool of literal strings,if the jvm find "test" exists,then it will use the exists one,in example 1,the "test" strings are all point to the same memory address,so the example 1 will return true.
in the other hand,in the example 2,the method of substring() execute in the runtime time,
in the case of "test" == "!test".substring(1),the pool will create two string object,"test"
and "!test",so they are different reference objects,so this case will return false,in the case of "test" == "!test".substring(1).intern(),the method of intern() will put the ""!test".substring(1)" to the pool of literal strings,so in this case,they are same reference objects,so will return true.

南巷近海 2024-08-20 20:37:19

http://en.wikipedia.org/wiki/String_interning

字符串驻留是一种仅存储的方法每个不同字符串值的一份副本,该值必须是不可变的。驻留字符串使某些字符串处理任务更加节省时间或空间,但代价是在创建或驻留字符串时需要更多时间。不同的值存储在字符串内部池中。

http://en.wikipedia.org/wiki/String_interning

string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

Bonjour°[大白 2024-08-20 20:37:19

内部字符串避免重复的字符串。实习可以节省 RAM,但代价是需要更多的 CPU 时间来检测和替换重复的字符串。无论有多少引用指向它,每个已被保留的字符串都只有一份副本。由于字符串是不可变的,如果两个不同的方法偶然使用同一个字符串,它们可以共享同一个字符串的副本。将重复字符串转换为共享字符串的过程称为 interning.String.intern(),它为您提供规范主字符串的地址。您可以使用简单的 == (比较指针)来比较 interned 字符串,而不是使用 equals 来一一比较字符串的字符。由于字符串是不可变的,因此实习进程可以自由地进一步节省空间,例如,当“pot”作为其他文字(例如“hippopotamus”)的子字符串存在时,不为“pot”创建单独的字符串文字。

要查看更多信息http://mindprod.com/jgloss/interned.html

Interned Strings avoid duplicate Strings. Interning saves RAM at the expense of more CPU time to detect and replace duplicate Strings. There is only one copy of each String that has been interned, no matter how many references point to it. Since Strings are immutable, if two different methods incidentally use the same String, they can share a copy of the same String. The process of converting duplicated Strings to shared ones is called interning.String.intern() gives you the address of the canonical master String. You can compare interned Strings with simple == (which compares pointers) instead of equals which compares the characters of the String one by one. Because Strings are immutable, the intern process is free to further save space, for example, by not creating a separate String literal for "pot" when it exists as a substring of some other literal such as "hippopotamus".

To see more http://mindprod.com/jgloss/interned.html

谈情不如逗狗 2024-08-20 20:37:19
String s1 = "Anish";
        String s2 = "Anish";

        String s3 = new String("Anish");

        /*
         * When the intern method is invoked, if the pool already contains a
         * string equal to this String object as determined by the
         * method, then the string from the pool is
         * returned. Otherwise, this String object is added to the
         * pool and a reference to this String object is returned.
         */
        String s4 = new String("Anish").intern();
        if (s1 == s2) {
            System.out.println("s1 and s2 are same");
        }

        if (s1 == s3) {
            System.out.println("s1 and s3 are same");
        }

        if (s1 == s4) {
            System.out.println("s1 and s4 are same");
        }

输出

s1 and s2 are same
s1 and s4 are same
String s1 = "Anish";
        String s2 = "Anish";

        String s3 = new String("Anish");

        /*
         * When the intern method is invoked, if the pool already contains a
         * string equal to this String object as determined by the
         * method, then the string from the pool is
         * returned. Otherwise, this String object is added to the
         * pool and a reference to this String object is returned.
         */
        String s4 = new String("Anish").intern();
        if (s1 == s2) {
            System.out.println("s1 and s2 are same");
        }

        if (s1 == s3) {
            System.out.println("s1 and s3 are same");
        }

        if (s1 == s4) {
            System.out.println("s1 and s4 are same");
        }

OUTPUT

s1 and s2 are same
s1 and s4 are same
拥醉 2024-08-20 20:37:19
String p1 = "example";
String p2 = "example";
String p3 = "example".intern();
String p4 = p2.intern();
String p5 = new String(p3);
String p6 = new String("example");
String p7 = p6.intern();

if (p1 == p2)
    System.out.println("p1 and p2 are the same");
if (p1 == p3)
    System.out.println("p1 and p3 are the same");
if (p1 == p4)
    System.out.println("p1 and p4 are the same");
if (p1 == p5)
    System.out.println("p1 and p5 are the same");
if (p1 == p6)
    System.out.println("p1 and p6 are the same");
if (p1 == p6.intern())
    System.out.println("p1 and p6 are the same when intern is used");
if (p1 == p7)
    System.out.println("p1 and p7 are the same");

当独立创建两个字符串时,intern() 允许您比较它们,并且如果引用之前不存在,它还可以帮助您在字符串池中创建引用。

当您使用String s = new String(hi)时,java会创建该字符串的一个新实例,但是当您使用String s = "hi"时,java会检查是否存在是否是代码中单词“hi”的实例,如果存在,则仅返回引用。

由于比较字符串是基于引用的,因此 intern() 可以帮助您创建引用并允许您比较字符串的内容。

当您在代码中使用 intern() 时,它会清除引用同一对象的字符串所使用的空间,并仅返回内存中已存在的同一对象的引用。

但如果是 p5,当您使用时:

String p5 = new String(p3);

仅复制 p3 的内容,并新创建 p5。所以它没有被拘留。

所以输出将是:

p1 and p2 are the same
p1 and p3 are the same
p1 and p4 are the same
p1 and p6 are the same when intern is used
p1 and p7 are the same
String p1 = "example";
String p2 = "example";
String p3 = "example".intern();
String p4 = p2.intern();
String p5 = new String(p3);
String p6 = new String("example");
String p7 = p6.intern();

if (p1 == p2)
    System.out.println("p1 and p2 are the same");
if (p1 == p3)
    System.out.println("p1 and p3 are the same");
if (p1 == p4)
    System.out.println("p1 and p4 are the same");
if (p1 == p5)
    System.out.println("p1 and p5 are the same");
if (p1 == p6)
    System.out.println("p1 and p6 are the same");
if (p1 == p6.intern())
    System.out.println("p1 and p6 are the same when intern is used");
if (p1 == p7)
    System.out.println("p1 and p7 are the same");

When two strings are created independently, intern() allows you to compare them and also it helps you in creating a reference in the string pool if the reference didn't exist before.

When you use String s = new String(hi), java creates a new instance of the string, but when you use String s = "hi", java checks if there is an instance of word "hi" in the code or not and if it exists, it just returns the reference.

Since comparing strings is based on reference, intern() helps in you creating a reference and allows you to compare the contents of the strings.

When you use intern() in the code, it clears of the space used by the string referring to the same object and just returns the reference of the already existing same object in memory.

But in case of p5 when you are using:

String p5 = new String(p3);

Only contents of p3 are copied and p5 is created newly. So it is not interned.

So the output will be:

p1 and p2 are the same
p1 and p3 are the same
p1 and p4 are the same
p1 and p6 are the same when intern is used
p1 and p7 are the same
許願樹丅啲祈禱 2024-08-20 20:37:19

string intern() 方法用于在字符串常量池中创建堆字符串对象的精确副本。字符串常量池中的字符串对象会自动实习,但堆中的字符串对象则不会。创建实习生的主要用途是节省内存空间并更快地执行字符串对象的比较。

来源: java 中的 string intern 是什么?

string intern() method is used to create an exact copy of heap string object in string constant pool. The string objects in the string constant pool are automatically interned but string objects in heap are not. The main use of creating interns is to save the memory space and to perform faster comparison of string objects.

Source : What is string intern in java?

幸福不弃 2024-08-20 20:37:19

正如你所说,字符串intern()方法首先会从字符串池中查找,如果找到,那么它将返回指向该字符串的对象,或者将一个新的字符串添加到池中。

    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = "Hello".intern();
    String s4 = new String("Hello");

    System.out.println(s1 == s2);//true
    System.out.println(s1 == s3);//true
    System.out.println(s1 == s4.intern());//true

s1s2是两个指向字符串池“Hello”的对象,使用"Hello".intern()会发现<代码>s1和s2。因此,"s1 == s3" 返回 true,s3.intern() 也返回 true。

As you said, that string intern() method will first find from the String pool, if it finds, then it will return the object that points to that, or will add a new String into the pool.

    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = "Hello".intern();
    String s4 = new String("Hello");

    System.out.println(s1 == s2);//true
    System.out.println(s1 == s3);//true
    System.out.println(s1 == s4.intern());//true

The s1 and s2 are two objects pointing to the String pool "Hello", and using "Hello".intern() will find that s1 and s2. So "s1 == s3" returns true, as well as to the s3.intern().

影子是时光的心 2024-08-20 20:37:19
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String s1 = "test";
    String s2 = new String("test");
    System.out.println(s1==s2);              //false
    System.out.println(s1==s2.intern());    //true --> because this time compiler is checking from string constant pool.
}
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String s1 = "test";
    String s2 = new String("test");
    System.out.println(s1==s2);              //false
    System.out.println(s1==s2.intern());    //true --> because this time compiler is checking from string constant pool.
}
分開簡單 2024-08-20 20:37:19

通过堆对象引用,如果我们想得到对应的字符串常量池对象引用,那么我们应该使用intern()

String s1 = new String("Rakesh");
String s2 = s1.intern();
String s3 = "Rakesh";

System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // true

Pictorial View
输入图片此处描述

第 1 步:
在堆和字符串常量池中创建带有数据“Rakesh”的对象。另外 s1 始终指向堆对象。

第 2 步:
通过使用堆对象引用 s1,我们尝试使用 intern() 获取相应的字符串常量池对象引用 s2

第 3 步:
有意在字符串常量池中创建一个包含数据“Rakesh”的对象,通过名称 s3 引用

作为“==”运算符,用于引用比较。

s1==s2 得到false

s2==s3 得到true

希望这有帮助!

By using heap object reference if we want to get corresponding string constant pool object reference, then we should go for intern()

String s1 = new String("Rakesh");
String s2 = s1.intern();
String s3 = "Rakesh";

System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // true

Pictorial View
enter image description here

Step 1:
Object with data 'Rakesh' get created in heap and string constant pool. Also s1 is always pointing to heap object.

Step 2:
By using heap object reference s1, we are trying to get corresponding string constant pool object referenc s2, using intern()

Step 3:
Intentionally creating a object with data 'Rakesh' in string constant pool, referenced by name s3

As "==" operator meant for reference comparison.

Getting false for s1==s2

Getting true for s2==s3

Hope this help!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文