java 关于字符串实例化的最佳实践好奇心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15646188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:24:01  来源:igfitidea点击:

Best practice curiosity regarding String instantiation

javastringinstantiation

提问by Bogdan M.

I was reading some advices about best practices in java and I got the following idea which made me curious

我正在阅读一些关于 Java 最佳实践的建议,我得到了以下让我很好奇的想法

Also whenever you want to instantiate a String object, never use its constructor but always instantiate it directly.

此外,无论何时您想实例化 String 对象,都不要使用其构造函数,而应直接实例化它。

For example:

例如:

//slow instantiation
String slow = new String("Yet another string object");

//fast instantiation
String fast = "Yet another string object";

Why is this? doesn't the 'fast' call the default string constructor?

为什么是这样?'fast' 不调用默认的字符串构造函数吗?

回答by Maroun

When you use newyou get a new string object, but if you use string literalthen see here:

当你用new你得到一个新的字符串对象,但是如果你使用字符串文本,然后在这里看到

In computer science, 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. The single copy of each string is called its 'intern' and is typically looked up by a method of the string class, for example String.intern() in Java. All compile-time constant strings in Java are automatically interned using this method.

在计算机科学中,字符串实习是一种仅存储每个不同字符串值的一个副本的方法,该副本必须是不可变的。驻留字符串使一些字符串处理任务在时间或空间上更加高效,代价是在创建或驻留字符串时需要更多时间。不同的值存储在字符串实习生池中。每个字符串的单个副本称为其“实习生”,通常由字符串类的方法查找,例如 Java 中的 String.intern()。Java 中的所有编译时常量字符串都使用此方法自动实习。

If you do:

如果你这样做:

String a = "foo";
String b = "foo";

Then a==bis true!

然后a==b真的

A String will be createdonly if it hasn't been interned. An object will be created in the first time, and it'll be stored in a place called the String constant pool.

只有当它没有被实习时才会创建一个字符串。第一次会创建一个对象,并将其存储在一个名为String 常量池的地方。

But using the newwhich will create a different object for each string, will output false.

但是使用new将为每个字符串创建不同对象的 将输出false

String a = new String("foo");
String b = new String("foo");

Now a==bis false.

现在a==b假的

So when using literal it is easier to read, plus easier for the compiler to make optimizations. So.. use it when you can.

所以当使用文字时,它更容易阅读,而且编译器更容易进行优化。所以..尽可能使用它。

回答by Nivas

The JVM maintains a pool of Stringliterals for optimizations. When you create a Stringusing the constructor,

JVM 维护一个String用于优化的文字池。当您String使用构造函数创建一个时,

String s1 = new String("foo");

A new Stringobject is created, and the literal "foo"is added to the pool. After this, anytime you use "foo"in your code, the "foo"refers to the item in the pool and a new object is not created. Since Stringis immutable, this does not create any problems.

String创建一个新对象,并将文字"foo"添加到池中。此后,无论何时"foo"在代码中使用,都将"foo"引用池中的项目,并且不会创建新对象。由于String是不可变的,这不会造成任何问题。

So when you create a Stringusing the "shortcut":

因此,当您String使用“快捷方式”创建一个时:

String s2 = "foo"

the JVM looks into the pool, if "foo"already exists there, it will make s2refer to the item in the pool.

JVM 查看池中,如果"foo"已经存在,它将s2引用池中的项目。

This is a major difference with possible performance impacts: The constructor always creates an object, and adds the literal to the pool if it is not already present there. The shortcut refers to the item in the pool and creates a new object only if the liuteral is not in the pool.

这是一个可能影响性能的主要区别:构造函数总是创建一个对象,如果它不存在,则将文字添加到池中。快捷方式引用池中的项目,并且仅当 liuteral 不在池中时才创建新对象。