U
user
if i want to compare a string str to something like... the letter f,
what do i do?
(str == "f") seems to return false everytime.
what do i do?
(str == "f") seems to return false everytime.
The == operator compares String pointers, not values. Useif i want to compare a string str to something like... the letter f,
what do i do?
(str == "f") seems to return false everytime.
Lars Enderin said:user skrev:
The == operator compares String pointers, not values. Use
"f".equals(str). The comparison order avoids null references.
Tim said:And ... for an encore ... the "byte" data type is signed!!
(There are a number of completely mad design decisions in the Java language
but these two have got to be the worst ...
I have found it a very difficult lesson to learn but == in Java doesuser said:if i want to compare a string str to something like... the letter f,
what do i do?
(str == "f") seems to return false everytime.
I have found it a very difficult lesson to learn but == in Java does
not have the same meaning as equals in other languages. If you want to
test for equality you need to use the equals method of the object str
so you need to test as follows:
if (str.equals"f") {...
or, God forbid,
if ("f".equals(str)) {...
It seems that origin of this is that it is much easier, when writing
an interpreter that is meant to work in a tiny "set top box" computer,
to test for equality between two pointers to an object than to test if
a human interpretation of the two objects are equal. Consequently,
unless the arguments are primitive types, == just checks to see if the
two arguments are the same object.
I don't dispute your two concepts of equality but you have usedEric Sosman said:David Segall wrote On 07/31/06 10:36,:
It's not a matter of easier or harder, but of modelling
the real world satisfactorily. The real world presents us
with multiple different notions of "equality," two of which
are supported by Java:
- Identity: Two objects A and B are "equal" if they are
in fact the same object under different names. "David"
and "Mr. Segall" refer to the same object (granting a
suitable universe of discourse), so they are equal.
This is the "equality" Java's == operator tests.
- Equivalence: Two objects A and B are "equal" if they
are of the same class and have the same "value." This
bottle of Pirelli's Miracle Elixir contains 100 ml. of
that efficacious fluid, that other bottle holds the
same amount of the same stuff, so the two bottles are
"equal" in the sense of being equivalent. This is the
notion the equals() method tests (depending, of course,
on how the class defines the method).
If you are having trouble separating these two notions,
consider your wallet for a moment. By pure coincidence, it
happens to contain the same amount of money as my wallet.
The two wallets are therefore equal in purchasing power; does
that mean they are the same wallet? If so, hand over your
wallet immediately: it's mine!
David Segall said:I don't dispute your two concepts of equality but you have used
sleight of hand to claim my wallet. I have no objection to being
forced to explicitly compare wallet.colour, wallet.size etc in
addition to wallet.value to protect my wallet.
Java confuses beginners
because it assigns a default property (.value) to a String object in
the equals() method but omits that property and insists on using the
unfamiliar concept of "identity" with the == operator.
This contrasts
with most (all?) other languages which is why I concluded that it was
done for the benefit of the interpreter programmer rather than the
application programmer.
David said:It seems that origin of this is that it is much easier, when writing
an interpreter that is meant to work in a tiny "set top box" computer,
to test for equality between two pointers to an object than to test if
a human interpretation of the two objects are equal. Consequently,
unless the arguments are primitive types, == just checks to see if the
two arguments are the same object.
[...]
I don't dispute your two concepts of equality but you have used
sleight of hand to claim my wallet.
I have no objection to being
forced to explicitly compare wallet.colour, wallet.size etc in
addition to wallet.value to protect my wallet. Java confuses beginners
because it assigns a default property (.value) to a String object in
the equals() method but omits that property and insists on using the
unfamiliar concept of "identity" with the == operator.
This contrasts
with most (all?) other languages
which is why I concluded that it was
done for the benefit of the interpreter programmer rather than the
application programmer.
Eric said:The signedness of `byte' is unfortunate, and is possibly
a product of the hobgoblin of little minds. But keeping the
notions of `==' and `.equals()' separate is another matter; if
you consider it "mad" I suspect you haven't thought about it
enough. ("People go mad if they think too much.")
Keeping the notions of "equal value" and "refer to same object" separate
is good. But I sometimes wonder if the operators could have been
allocated in such a way as to cause least confusion to learners.
e.g. == for the extremely commonly meant 'has same string value as' and
.sameObject() for the far less used 'are both references to the same thing'.
Eric Sosman said:Ian Wilson wrote On 08/15/06 04:55,:
How would you test for a null reference in this scheme?
someString.equals(null) // NPE if someString is null
null.equals(someString) // doesn't compile
((String)null).equals(someString) // NPE always
I guess the test *could* be made:
static boolean isReferenceNull(Object ref) {
try {
ref.getClass(); // NPE if ref is null
return false; // no NPE -> not null
}
catch (NullPointerException ex) {
return true; // only one way to get here
}
}
... but this is vile.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.