String intern() method in Java. Why we use it?

Briefly, Java’s String class has a public method intern() that returns a canonical representation for the string object. Java’s String class privately maintains a pool of strings, where String literals are automatically interned. When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there 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 类 的 intern()方法 返回字符串对象的规范表示。 Java String类私有地维护一个字符串池,位于常量池中String常量自动调用intern方法。当对String对象调用intern方法,先从常量池找,没有的话会将其添加到常量池,并返回引用。

The pool of strings in Java is maintained for saving space and for faster comparisons. Two string literals can be compared by == operator which is faster than equals(), while two String objects cannot be compared by == operator. You should use equals() method to compare two String objects.

常量池可以节约空间,并且常量比较更加迅速。池中的两个字符串常量比较只需要用==,不需要使用equals,==比equals更快。
然而new出来的字符串对象想要比较内容要使用equals,不能用==,==比较的是地址。(new出来的字符串对象是放在堆上的)

The intern() method helps in comparing two String objects with == operator by looking into the pre-existing pool of string literals, no doubt it is faster than equals() method. Normally Java programmers are advised to use equals(), not ==, to compare two strings. This is because == operator compares memory locations, while equals() method compares the content stored in two objects. Java’s intern() method provides us an opportunity to intern strings when they are not constants and either objects or created at run time, and we want to quickly compare them to other interned strings. Of course, this will save time if we make lot of such comparisons in our program.

那么想要对new出来的字符串对象比较内容,又使用==,那就先调用intern方法。一般java开发人员被建议String类使用equals比较,equals比较的是内容,不要使用==,就是因为==比较地址。Java’s intern() 方法给了我们机会,可以用==比较对象或常量,只要它们都是返回字符串对象的规范表示。当然,这样肯定更节约时间如果有大量比较。

Remember that we only need to intern strings when they are not constants, and we want to be able to quickly compare them to other interned strings. The intern() method should be used on strings constructed with new String() in order to compare them by == operator. Let’s take a look at the following Java program, and you will understand when the intern() method is proven to be useful.

仅当String不是常量池中,我们又想用字符串对象的规范表示进行快速比较,那么使用intern方法。

当使用new String构造方法又想通过==快速比较字符串时,应该使用intern方法。让我们看下下面的java程序,你会明白使用intern有多有用

/* InternDemo.java: Demonstrating intern method*/
public class InternDemo
{
  public static void main(String[] args)
  {
    String s1 = "Hello";
    String s2 = s1;
    String s3 = new String("Hello");
    String s4 = "lo";

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

    //Strings computed by concatenation at 
    //run-time are newly created and therefore distinct.
    System.out.println("Hello" == "Hel"+s4); //flase

    //s3 is not literal, so distinct
    System.out.println(s3 == ("Hel"+s4).intern()); //false

    //The result of explicitly interning a computed string is the same 
    //string as any pre-existing literal string with the same contents.
    System.out.println(s1 == s3.intern()); //true
    System.out.println("Hello" == ("Hel"+s4).intern()); //true
    System.out.println(s1 == ("Hel"+s4).intern()); //true
    System.out.println(s3.intern() == ("Hel"+s4).intern()); //true
  }
}

OUTPUT
======
D:\JavaPrograms>javac InternDemo.java
D:\JavaPrograms>java InternDemo
true
true
false
false
false
true
true
true
true

Above program demonstrates that strings created at run time are not interned by default, and we have to invoke intern() method to compare them by == operator.
上面代码示例,运行时产生的字符串必须强制使用intern,如果通过==进行比较

Moreover, look at the following points mentioned in Java Language Specification about literal strings:

Literal strings within the same class in the same package represent references to the same String object.
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions are computed at compile time and then treated as if they were literals.
Strings computed by concatenation at run-time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值