检查 2 个不同实例的相同实例(包含示例)
我在下面的代码中使用了 == 并打印出“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是由于
String
实习。Java(JVM)保留一个
String
文字的集合,用于节省内存。因此,每当您创建一个String
时,就像这样:Java 'interns' 字符串。但是,如果您像这样创建
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 aString
like so:Java 'interns' the string. However, if you create the
String
like so: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.本文将详细解释它:
什么是Java中==和equals()的区别?
This article will explain it in details:
What is the difference between == and equals() in Java?
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 makea
refer to it.When the interpreter sees
String b = "boy";
, it first checks to see ifstring
"boy"
is present in the string pool, since it is present, no new object is created andb
is made to refer to the same object thata
is referring to.Since both references contain the same content they pass the equality test.
因为运行时会有一个字符串池,当需要分配新的常量字符串时,运行时会查找池内,如果池包含它,则将变量设置为指向池内的同一个 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
每当我们创建一个如下所示的字符串时:
JVM会检查字符串常量池中的
str2 = "abc"
,如果它存在,那么它不会创建一个新的String,而是指向常量池中的字符串1。字符串常量池。但在这种情况下,
String str = new String("abc");
它总是会创建一个新的字符串对象,但我们可以使用intern()
函数来强制JVM查看字符串常量池。Whenever we create a string like below :
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 useintern()
function to force JVM to look into the string constant pool.正如上面正确解释的,在
'=='
比较的情况下,运行时将在String
池中查找字符串是否存在。但是,在垃圾收集期间或内存问题期间,虚拟机很可能会破坏字符串池。因此,"=="
运算符可能会也可能不会返回正确的值。课程 - 始终使用
equals()
进行比较。As rightly explained above, in the case of
'=='
comparison, the runtime will look into theString
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.