基本Java问题:字符串相等 [英] Basic Java question: String equality
本文介绍了基本Java问题:字符串相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class A {
static String s1 = "I am A";
public static void main(String[] args) {
String s2 = "I am A";
System.out.println(s1 == s2);
}
}
以上程序输出true。两个是两个不同的标识符/对象输出是如何真?
Above program outputs "true". Both are two different identifiers/objects how the output is "true" ?
我的理解是JVM将为每个对象创建不同的引用,如果是这样,输出如何真正?
My understanding is that the JVM will create different reference for each object, if so how the output is true?
推荐答案
Java管理 String
文字池。它尽可能地重用这些文字。因此,这两个对象实际上是相同的 String
对象, ==
返回true。
Java manages a String
literal pool. It reuses these literals when it can. Therefore the two objects are actually the same String
object and ==
returns true.
我相信这叫做 string interning
这篇关于基本Java问题:字符串相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文