我不明白这个 (“string”==“string”) 示例

发布于 2024-12-05 00:43:48 字数 599 浏览 0 评论 0原文

我在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 技术交流群。

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

发布评论

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

评论(5

空‖城人不在 2024-12-12 00:43:48

是的,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 case evt.getPropertyName() is null. Note that the implementation of String.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.

肥爪爪 2024-12-12 00:43:48

我们正在看哪个演示?

这解释了 equals() 与 ==

http://www.java-samples.com /showtutorial.php?tutorialid=221

了解 equals( ) 方法和 == 运算符执行两种不同的操作非常重要。正如刚才所解释的,equals() 方法比较 String 对象内的字符。 == 运算符比较两个对象引用以查看它们是否引用同一个实例。以下程序显示两个不同的 String 对象如何包含相同的字符,但对这些对象的引用不会比较为相等:

因此,在您的特定示例中,它正在比较引用以查看它们是否是相同的引用,而不是查看是否相同我相信字符串字符匹配。

Which demo are we looking at?

This explains equals() vs ==

http://www.java-samples.com/showtutorial.php?tutorialid=221

It is important to understand that the equals( ) method and the == operator perform two different operations. As just explained, the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal:

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.

我的鱼塘能养鲲 2024-12-12 00:43:48

该代码的正确版本应该是:

if ("progress".equals(evt.getPropertyName()))

由于 JVM 处理字符串常量的方式,这可能会起作用。每个字符串常量都是intern()ed。因此,如果 evt.getPropertyName() 返回对字符串常量的引用,则使用 == 会起作用。但这是不好的形式,一般来说是行不通的。

The correct version of this code should be:

if ("progress".equals(evt.getPropertyName()))

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.

一曲琵琶半遮面シ 2024-12-12 00:43:48

仅当 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.

萝莉病 2024-12-12 00:43:48

在大多数情况下,比较String时,使用equals是最好的。但是,如果您知道您将比较完全相同的 String 对象(不仅仅是具有相同内容的两个字符串),或者您完全处理常量 String >s你真的很关心性能,使用==会比使用equals快一些。您通常应该使用 equals,因为您通常不太关心性能而不会考虑使用 == 的所有其他先决条件。

在本例中,作者 进度demo 可能应该使用 equals - 该代码对性能并不是特别关键。然而,在这种特殊情况下,代码将完全处理常量字符串,因此虽然它可能不是最佳选择,特别是对于演示来说,但它是一个有效的选择。

In most cases, when comparing Strings, using equals is best. However, if you know you'll be comparing the exact same String objects (not just two strings that have the same content), or if you're dealing entirely with constant Strings and you really care about performance, using == will be somewhat faster than using equals. You should normally use equals 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.

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