用“==”比较两个字符串:什么时候有效?

发布于 2024-12-06 00:02:48 字数 252 浏览 0 评论 0原文

假设你有三个字符串,

String s1 = "string one";
String s2 = new String("string one");
String s3 = "string one";

我知道 s1 == s2false,但我在某处读到 s1 == s3。这是正确的吗?为什么或为什么不呢?

Say you have three strings,

String s1 = "string one";
String s2 = new String("string one");
String s3 = "string one";

I know it is true that s1 == s2 is false, but I read somewhere that s1 == s3 is true. Is this correct? Why or why not?

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

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

发布评论

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

评论(3

陈甜 2024-12-13 00:02:48

字符串文字会自动保留。因此 s1 == s3 为真。字符串既可以在字符串常量池中创建,也可以在堆空间中创建。如果你实习了在堆中创建的字符串,则该字符串将位于字符串常量池中。

当您创建字符串文字(String s1 =“string one”)时,该字符串将在字符串常量池中创建。此外,字符串常量池不存储重复项。所以当你说,

String s1 = "string one";
String s3 = "string one";

s1 和 s3 都将指向字符串常量池中字符串的同一个实例。所以 s1.equals(s3) 将为 true。并且 s1 == s3 也成立;因为两个指针是相同的。

但是,当您使用“new”构造函数实例化字符串时

String s2 = new String("string one");

,会在堆空间中创建 s2。堆空间与字符串常量池是不同的内存区域

因此,当 s1.equals(s2) 为 true 时,s1 == s2 为 false;因为它们将指向不同的内存区域。

但是,您可以通过调用 intern() 函数来转换使用“new”构造函数创建的字符串,使其移至字符串常量池。所以s2.intern()会返回字符串常量池中的一个字符串;尽管s2最初是在堆中创建的。

String literals are interned automatically. Hence s1 == s3 is true. Strings can either be created in the string constant pool or they can be created in the heap space. If you intern a string created in the heap, the string will be in the string constant pool.

When you create a string literal (String s1 = "string one"), the string is created in the string constant pool. Additionally, the string constant pool doesn't store duplicates. So when you say,

String s1 = "string one";
String s3 = "string one";

Both s1 and s3 will be pointing to the same instance of the string in the string constant pool. So s1.equals(s3) will be true. And s1 == s3 also is true; since both the pointers are the same.

However, when you instantiate a string using the "new" constructor

String s2 = new String("string one");

then s2 is created in the heap space. The heap space is a different area of memory than the string constant pool

So while s1.equals(s2) is true, s1 == s2 is false; since they will be pointing to different areas of memory.

However, you can convert a string created using the "new" constructor to make it move to the string constant pool by invoking the intern() function. So s2.intern() will return a string in the string constant pool; although s2 was originally created in the heap.

黎歌 2024-12-13 00:02:48

是的,确实如此,因为字符串文字都是被保留的。阅读 String.intern() 了解更多详细信息。

因此,这些都是同一个对象(并且与 == 比较相等):

s1
s3
s1.intern()
s2.intern()
s3.intern()

Yes, that is true, because string literals are all interned. Read the documentation for String.intern() for more details.

As a consequence, these are all the same object (and will compare equal with ==):

s1
s3
s1.intern()
s2.intern()
s3.intern()
z祗昰~ 2024-12-13 00:02:48

以上是如何进行的。

你应该知道为什么。

A) 编译时声明的字符串变量引用常量池中的常量

B) 通过方法引用的字符串变量结果引用堆空间中的对象。

这都是因为 JVM 规范及其内存设计。

A)是因为字符串是不可变的。当java编译类和jvm加载类时,jvm在编码时发现了你声明的一个String变量。由于它是常量,jvm将常量放入“运行时常量池”内存区域。并且,常数是唯一的。

B) 非常简单。因为jvm运行时变量使用堆空间。

above is HOW.

and You should know why.

A) string varaible declared in compile time ref to constant in constant pool

B) string varaible result by method ref to Object in heap space .

it all because JVM specfication and it's memory design.

A) is because strings are immutable。when java compile class and jvm load class , jvm found one String variable declared by your in coding time。 cause it is constant, jvm put the constant to "Runtime Constant Pool" memory area. and, constant is unique.

B) is verysimple. because jvm runtime variable use heap space.

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