Java的String.intern()的用途是什么? [英] What is the purpose of Java's String.intern()?

查看:67
本文介绍了Java的String.intern()的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有两种用Java创建String的方法:

I know there are two ways of creating String in Java:

String a = "aaa";
String b = new String("bbb");

使用第一种方法,Java肯定会在字符串池中创建一个String对象,并使 a 引用该对象.(假设"aaa"以前不在池中.)

With the first way Java will definitely create a String object in the string pool and make a refer to it. (Assume "aaa" wan't in the pool before.)

使用第二种方法,将在堆中创建一个对象,但是jvm还将在字符串池中创建一个对象吗?

With the second method, an object will be created in the heap, but will jvm also create an object in the string pool?

在这篇帖子有关Java的字符串池的问题中,@ Jesper说:

In this post Questions about Java's String pool, @Jesper said:

如果您这样做:

String s = new String("abc");

那么池中将有一个String对象,该对象代表文字"abc",>并且将有一个单独的String对象(不在池中),其中包含>的副本.合并对象的内容.

then there will be one String object in the pool, the one that represents the literal "abc", > and there will be a separate String object, not in the pool, that contains a copy of the > content of the pooled object.

如果是这样,则每次使用 new String("bbb"); 时,对象"bbb"就会被删除.是在池中创建的,这意味着通过以上两种方式,java都会始终在池​​中创建一个字符串对象.那么 intern()的用途是什么?在文档 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern(),它说:

If that's true, then every time with the new String("bbb");, a object "bbb" is created in the pool, which means by either way above, java will always create a string object in the pool. Then what is intern() used for ? In the docs http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern(), it says:

调用intern方法时,如果池中已经包含等于equals(Object)方法确定的此String对象的字符串,则返回池中的字符串.否则,将此String对象添加到池中,并返回对此String对象的引用.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) 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.

这意味着在某些情况下池中没有字符串,这可能吗?哪个是真的?

That means there are cases that a string is not in the pool, is that possible ? Which one is true ?

推荐答案

我们的 String 对象可以通过两种方式进入池:

There are essentially two ways that our String objects can enter in to the pool:

  • 在源代码中使用文字,例如"bbb" .
  • 使用 intern .

intern 用于当您有一个不是来自池中的 String 时.例如:

intern is for when you have a String that's not otherwise from the pool. For example:

String bb = "bbb".substring(1); // substring creates a new object

System.out.println(bb == "bb");          // false
System.out.println(bb.intern() == "bb"); // true

或稍有不同:

System.out.println(new String("bbb").intern() == "bbb"); // true

新的String("bbb")确实创建了两个对象...

new String("bbb") does create two objects...

String fromLiteral = "bbb";                     // in pool
String fromNewString = new String(fromLiteral); // not in pool

...但是它更像是一个特例.因为 "bbb" 是指对象:

...but it's more like a special case. It creates two objects because "bbb" refers to an object:

字符串文字是对类 String [...]的实例的引用.

A string literal is a reference to an instance of class String [...].

此外,字符串文字总是引用类 String 的相同实例.

Moreover, a string literal always refers to the same instance of class String.

然后 new String(...)创建它的副本.

但是,有许多方法可以在不使用文字的情况下创建 String 对象,例如:

However, there are many ways String objects are created without using a literal, such as:

  • 所有执行某种突变的 String 方法.( substring split replace 等)
  • 从某种输入(例如 Scanner Reader )中读取 String .
  • 当至少一个操作数不是编译时常量时的串联.
  • All the String methods that perform some kind of mutation. (substring, split, replace, etc.)
  • Reading a String from some kind of input such as a Scanner or Reader.
  • Concatenation when at least one operand is not a compile-time constant.

intern 使您可以将它们添加到池中,或检索存在的对象(如果有的话).在大多数情况下,不需要插入 String s,但是可以将其用作优化,因为:

intern lets you add them to the pool or retrieve an existing object if there was one. Under most circumstances interning Strings is unnecessary but it can be used as an optimization because:

  • 它可让您与 == 进行比较.
  • 它可以节省内存,因为重复项可以被垃圾回收.

这篇关于Java的String.intern()的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆