Java String s1="Hello" 和 String s1=new String("Hello") 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19509242/
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-08-12 17:51:23  来源:igfitidea点击:

What is the Difference between String s1="Hello" and String s1=new String("Hello")?

java

提问by CMQ_14

What is the Difference between String s1="Hello"and String s1=new String("Hello")in Java?

JavaString s1="Hello"String s1=new String("Hello")Java 中的区别是什么?

If String s1="Hello"and String s2=new String("Hello"), will s1 == s2?

如果String s1="Hello"String s2=new String("Hello"),会s1 == s2吗?

回答by Masudul

If String s1="Hello" and String s2=new String("Hello"), will s1 == s2?

如果 String s1="Hello" 并且 String s2=new String("Hello"),那么 s1 == s2 吗?

Answer is: No.

答案是:没有

Because, s1 and s2 are different object and stringis immutable. So, s1 == s2will be falseand s1.equals(s2)will be true.

因为,s1 和 s2 是不同的对象并且string是不可变的。所以, s1 == s2将是falses1.equals(s2)将是true

回答by Shishigami

Here is a quote from Joshua Bloch's Effective Java regarding the use of "new String()" :

以下是 Joshua Bloch 的 Effective Java 中关于“new String()”使用的引用:

As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("stringette") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly. The improved version is simply the following:

String s = "stringette";

作为不该做什么的极端示例,请考虑以下语句:

String s = new String("stringette"); //不要这样做!

该语句每次执行时都会创建一个新的 String 实例,并且不需要创建这些对象。String 构造函数(“stringette”)的参数本身就是一个 String 实例,在功能上与构造函数创建的所有对象相同。如果这种用法发生在循环中或频繁调用的方法中,则可能会不必要地创建数百万个 String 实例。改进后的版本如下:

String s = "stringette";

回答by Whoppa

String myStr = "hello";
String myStr1 = "hello";

These both will evaluate to true when compared via double equals. However, they are not equal but rather both point to the same "literal string" in memory. This is NEVER how you compare contents of a String so don't let this fool you.

当通过双等于比较时,这两者都会评估为真。然而,它们并不相等,而是都指向内存中相同的“文字字符串”。这绝不是您比较字符串内容的方式,所以不要让这愚弄您。

String myStr = new String("hello");
String myStr1 = new String("hello");

Will evaluate to false because they both reference distinct objects with distinct memory addresses.

将评估为 false,因为它们都引用具有不同内存地址的不同对象。

Always always always use myStr.equals(myStr1)when comparing String's contents for equality. Remember ==only compared whether they reference the same object in memory.

use myStr.equals(myStr1)在比较 String 的内容是否相等时,始终始终。记住==只比较它们是否引用了内存中的同一个对象。

回答by Bohemian

Coding this:

编码这个:

String s1 = "Hello";

Causes the JVM to internthe String literal: Every usage of the same String literal will be a reference to the same object.

使 JVM实习字符串文字:每次使用相同的字符串文字将是对同一对象的引用。

Coding this:

编码这个:

String s2 = new String("Hello")

Will always create a new String object.

将始终创建一个新的 String 对象。

So, will s1 == s2?

那么,会s1 == s2吗?

No, never.

没有永不。

回答by Bette Devine

Your question's answer is no, please find the reason below:-

您的问题的答案是否定的,请在下面找到原因:-

  1. String s1 = "Hello". Here, hello string will be stored at a location and and s1 will keep a reference to it.
  2. String s2=new String("Hello") will create a new object, will refer to that one and will be stored at a different location.
  1. 字符串 s1 = "你好"。在这里,hello 字符串将存储在一个位置,并且 s1 将保留对它的引用。
  2. String s2=new String("Hello") 将创建一个新对象,将引用该对象并将其存储在不同的位置。

The condition, s1==s2 will compare memory locations which are now 2 different locations in memory. So it will come false.

条件 s1==s2 将比较内存位置,它们现在是内存中的 2 个不同位置。所以它会是假的。