【发布时间】:2013-11-08 07:25:49
【问题描述】:
class helloworld
{
public static void main(String args[]) {
String str1="hello";
String str2="world";
String str=str1+str2;
str.intern();
System.out.println(str=="helloworld");
}
}
o/p: 假
执行程序后,它会产生 false 作为输出。如果使用 equals() 而不是 "==" 它返回 true。为什么会这样?
2.在这种情况下,更改类名后,它会产生 true 作为输出。
class main
{
public static void main(String args[]) {
String str1="hello";
String str2="world";
String str=str1+str2;
str.intern();
System.out.println(str=="helloworld");
}
}
o/p:true
为什么使用 "==" 与类名进行黑白字符串比较会发生矛盾(如果比较字符串名称用作类名)?
【问题讨论】:
-
查找字符串实习
-
这只是让你放弃使用 == 并使用 String.equals()
-
其实这是一个非常有趣的问题,+1。伙计们,这不是您的想法,请参阅@DavidWallace 的回答。
-
这是一个非常有趣的问题。否决它的唯一原因是未能理解它。
-
有人试过上面给出的例子吗?对我来说,无论如何都会产生“假”!
标签: java classname string-interning