检查 2 个不同实例的相同实例(包含示例)

发布于 2024-09-17 22:41:11 字数 377 浏览 14 评论 0原文

我在下面的代码中使用了 == 并打印出“Equals!”,为什么?有人能解释为什么这两个不同的字符串 a 和 b 相等吗?

public class test
{
    public static void main()
    {
        String a = "boy";
        String b = "boy";

        if(a == b)
        {
            System.out.println("Equals!");
        }
        else
        {
            System.out.println("Does not equal!");
        }
    }
}

I use the == in the code below and prints out "Equals!", why? Can someone explain why these two different strings a and b are equal?

public class test
{
    public static void main()
    {
        String a = "boy";
        String b = "boy";

        if(a == b)
        {
            System.out.println("Equals!");
        }
        else
        {
            System.out.println("Does not equal!");
        }
    }
}

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

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

发布评论

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

评论(6

很酷又爱笑 2024-09-24 22:41:11

这是由于String实习

Java(JVM)保留一个String文字的集合,用于节省内存。因此,每当您创建一个 String 时,就像这样:

String s = "String";

Java 'interns' 字符串。但是,如果您像这样创建String

String s = new String("String");

Java 不会自动实习String。如果您以这种方式创建字符串,您的代码将产生不同的结果。

快速 Google 搜索 揭示了许多有关 String 实习的好资源。

This is due to String interning.

Java (The JVM) keeps a collection of String literals that is uses to save memory. So, whenever you create a String like so:

String s = "String";

Java 'interns' the string. However, if you create the String like so:

String s = new String("String");

Java will not automatically intern the String. If you created your strings this way, your code would produce different results.

A quick Google search reveals lots of good resources regarding String interning.

浅唱ヾ落雨殇 2024-09-24 22:41:11

本文将详细解释它:

什么是Java中==和equals()的区别?

执行完String a =
“男生”; JVM 添加了
将“boy”字符串添加到字符串中
池并在代码的下一行,它
再次遇到 String b = ”boy”;在这种情况下,JVM 已经
知道这个字符串已经是
在池中,所以它不会创建
新字符串。所以字符串 a 和 b 都指向同一个字符串,这意味着它们
指向相同的引用。

This article will explain it in details:

What is the difference between == and equals() in Java?

After the execution of String a =
“boy”; the JVM adds the
string “boy” to the string
pool and on the next line of the code, it
encounters String b = ”boy” again; in this case the JVM already
knows that this string is already
there in the pool, so it does not create a
new string. So both strings a and b point to the same string what means they
point to the same reference.

蓝色星空 2024-09-24 22:41:11

String a = "boy"; 将创建一个值为 ("boy") 的新字符串对象,将其放入字符串池中并让 a 引用它。

当解释器看到 String b = "boy"; 时,它首先检查字符串池中是否存在 string "boy",由于它存在,因此不会创建新对象,并且 b 会引用 a 所引用的同一对象。

由于两个引用包含相同的内容,因此它们通过了相等性测试。

String a = "boy"; will create a new string object with value ("boy"), place it in the string pool and make a refer to it.

When the interpreter sees String b = "boy";, it first checks to see if string "boy" is present in the string pool, since it is present, no new object is created and b is made to refer to the same object that a is referring to.

Since both references contain the same content they pass the equality test.

用心笑 2024-09-24 22:41:11

因为运行时会有一个字符串池,当需要分配新的常量字符串时,运行时会查找池内,如果池包含它,则将变量设置为指向池内的同一个 String 对象。

但你永远不应该依赖它来检查内容字符串是否相等。您应该使用以下方法:equals

Because the run time will have a string pool and when you need to assign a new constant string, the run time look inside the pool, if the pool contains it, then they set the variable point to the same String object inside the pool.

But you should never depends on this to check for content string equals. You should use the method: equals

你在看孤独的风景 2024-09-24 22:41:11

每当我们创建一个如下所示的字符串时:

String str1 = "abc";
String str2 = "abc";

JVM会检查字符串常量池中的str2 = "abc",如果它存在,那么它不会创建一个新的String,而是指向常量池中的字符串1。字符串常量池。

但在这种情况下,String str = new String("abc");它总是会创建一个新的字符串对象,但我们可以使用intern()函数来强制JVM查看字符串常量池。

Whenever we create a string like below :

String str1 = "abc";
String str2 = "abc";

JVM will check the str2 = "abc" in the string constant pool, if it is present then it wont create a new String instead it point to the string one in the string constant pool.

But in case of this String str = new String("abc"); it will always create a new String Object but we can use intern() function to force JVM to look into the string constant pool.

失去的东西太少 2024-09-24 22:41:11

正如上面正确解释的,在 '==' 比较的情况下,运行时将在 String 池中查找字符串是否存在。但是,在垃圾收集期间或内存问题期间,虚拟机很可能会破坏字符串池。因此,"==" 运算符可能会也可能不会返回正确的值。

课程 - 始终使用 equals() 进行比较。

As rightly explained above, in the case of '==' comparison, the runtime will look into the String pool for the existence of the string. However, it very much possible that during garbage collection, or during memory issues, the virtual machine might destroy the string pool. The "==" operator therefor might or might not return the correct value.

Lesson - Always use equals() for comparison.

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