I don't understand this ("string" == "string") example
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 开发者_JS百科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.
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.
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.
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.
This only would work if evt.getPropertyName() returns a constant string of value "progress". With constant string, I mean evaluated at compile-time.
In most cases, when comparing String
s, 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 String
s 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.
精彩评论