我不明白这个 (“string”==“string”) 示例
我在java教程页面上找到了这个java代码:
if ("progress" == evt.getPropertyName())
http:// download.oracle.com/javase/tutorial/uiswing/examples/components/index.html
这是如何工作的?我认为对于这种情况我们必须使用 equals()
方法(string.equals("bla")
)?我们也可以在这里使用equals()
吗?会更好吗?有什么想法吗?
编辑:所以如果equals()
会更好,那么我真的不明白为什么严肃的oracle教程页面没有使用它?另外,我不明白它为什么起作用,因为我认为字符串是一个对象。如果我说object == object
,那么这是一个大问题。
I found this java code on a java tutorial page:
if ("progress" == evt.getPropertyName())
http://download.oracle.com/javase/tutorial/uiswing/examples/components/index.html
How could this work? I thought we HAVE TO use the equals()
method for this situation (string.equals("bla")
)? Could we use equals()
here too? Would it be better? Any ideas?
Edit: So IF equals()
would be better, then I really don't understand why a serious oracle tutorial page didn't use it? Also, I don't understand why it's working because I thought a string is an object. If I say object == object
, then that's a big problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,equals() 肯定会更好、更正确。在 Java 中,为了提高性能,会智能地维护和重用字符串常量池。所以这个可以工作,但只有当 evt.getPropertyName() 确保返回常量时才能保证。
此外,如果
evt.getPropertyName()
为 null,更正确的版本将是"progress".equals(evt.getPropertyName())
。请注意,String.equals
的实现首先使用==
作为逐个字符比较之前的第一个测试,因此与原始版本相比,性能不会受到太大影响代码。Yes, equals() would definitely be better and correct. In Java, a pool of string constants is maintained and reused intelligently for performance. So this can work, but it is only guaranteed if evt.getPropertyName() is assured to return constants.
Also, the more correct version would be
"progress".equals(evt.getPropertyName())
, in caseevt.getPropertyName()
is null. Note that the implementation ofString.equals
starts with using==
as a first test before doing char-by-char comparison, so performance will not be much affected versus the original code.我们正在看哪个演示?
这解释了 equals() 与 ==
http://www.java-samples.com /showtutorial.php?tutorialid=221
因此,在您的特定示例中,它正在比较引用以查看它们是否是相同的引用,而不是查看是否相同我相信字符串字符匹配。
Which demo are we looking at?
This explains equals() vs ==
http://www.java-samples.com/showtutorial.php?tutorialid=221
So in your particular example, it is comparing the reference to see if they are the same reference, not to see if the string chars match I believe.
该代码的正确版本应该是:
由于 JVM 处理字符串常量的方式,这可能会起作用。每个字符串常量都是intern()ed。因此,如果 evt.getPropertyName() 返回对字符串常量的引用,则使用
==
会起作用。但这是不好的形式,一般来说是行不通的。The correct version of this code should be:
This could work because of the way that the JVM handles string constants. Each string constant is intern()ed. So if evt.getPropertyName() is returning a reference to a string constant than using
==
will work. But it is bad form and in general it will not work.仅当 evt.getPropertyName() 返回值“progress”的常量字符串时,此方法才有效。
对于常量字符串,我的意思是在编译时评估。
This only would work if evt.getPropertyName() returns a constant string of value "progress".
With constant string, I mean evaluated at compile-time.
在大多数情况下,比较
String
时,使用equals
是最好的。但是,如果您知道您将比较完全相同的String
对象(不仅仅是具有相同内容的两个字符串),或者您完全处理常量String
>s和你真的很关心性能,使用==
会比使用equals
快一些。您通常应该使用equals
,因为您通常不太关心性能而不会考虑使用==
的所有其他先决条件。在本例中,作者 进度demo 可能应该使用
equals
- 该代码对性能并不是特别关键。然而,在这种特殊情况下,代码将完全处理常量字符串,因此虽然它可能不是最佳选择,特别是对于演示来说,但它是一个有效的选择。In most cases, when comparing
String
s, usingequals
is best. However, if you know you'll be comparing the exact sameString
objects (not just two strings that have the same content), or if you're dealing entirely with constantString
s and you really care about performance, using==
will be somewhat faster than usingequals
. You should normally useequals
since you normally don't care about performance sufficiently to think about all the other prerequisites for using==
.In this case, the author of the progress demo should probably have used
equals
- that code isn't especially performance-critical. However, in this particular case, the code will be dealing entirely with constant strings, so whilst it's probably not the best choice, especially for a demo, it is a valid choice.