
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
>> first of all, why would it not be printing out what it really is already
>> especially since when I print it the character that is printed is
[quoted text clipped - 11 lines]
>
> note the single quotes to enclose a char as opposed to a String.
That works but if I have to do it that way why does the Character class
have an equals() method to use? Not to mention the fact that the if()
worked okay if I used an arbitrary "m" instead of "=" in my equals() so
it seemed capable of doing Character to String comparisons that way. any
further insights?
thanks
Aleksey Gureev - 19 Mar 2006 10:04 GMT
equals() method is generally used to compare objects of the same type.
If the Character class knows how to compare its objects with String
objects -- that's fine, but basically, as String is not a subclass of
Character, it wouldn't work. Just ask yourself how single character
could be compared to, say "Hello"?
The best what you can do is:
---
Character ch = new Character(dnArray[0].charAt(3));
if ( ch.charValue == '=' ) {
displayString = dnArray[0].substring(1,dnArray[0].length());
} else {
displayString = dnArray[0].substring(4,dnArray[0].length());
}
---
or, in fact
---
displayString = dnArray[0].subscript(dnArray[0].charAt(3) == '=' ? 1 :
4);
---
The last one will do the same as your original code.
Roland de Ruiter - 19 Mar 2006 12:14 GMT
>>> first of all, why would it not be printing out what it really is
>>> already especially since when I print it the character that is
[quoted text clipped - 19 lines]
>
> thanks
The javadoc of Character's equal method tells it all:
"public boolean equals(Object obj)
Compares this object against the specified object. The result is true if
and only if the argument is not null and is a Character object that
represents the same char value as this object.
"
See
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#equals(java.lan
g.Object)>
You were comparing a Character object with the String object "=". In all
possible cases of a (non-null) Character object, the result of the
equals method will return false.
Unless you have compelling reasons to use a *Character* _object_, its
better to use the *char* _primitive type_ instead.

Signature
Regards,
Roland
Roedy Green - 19 Mar 2006 18:59 GMT
>That works but if I have to do it that way why does the Character class
>have an equals() method to use?
I repeat, comparing two characters. If you try to use it for comparing
a Character and a String or Character and a Onion it will say they
are not equal.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
This problem makes me consider a question that has been rolling around
in my head for a little over a week. Definitions of equals return
false when passed in parameter is of a different type than method's
"this" object. That's the way I've seen equals implemented, that's the
way I've implemented it in many a class.
A couple of weeks ago I noticed I wrote the equals of four different
classes where an exception would be thrown if type of passed in
parameter was different than method's "this" object. I immediately
considered changing the code, thinking it is good that I review what I
write. However, before changing source I realized I liked the
exception for the reason that I never will intentionally invoke those
classes equals with other types of objects. For those objects I never
even want to put them into HashMap or Hashtable with other types. So
basically I want to know of the error if I accidentally write code that
compares instances of those classes to instances of other classes
instead of silent false returns. But that is not the common way to
write equals.
What do you think?
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Aleksey Gureev - 19 Mar 2006 14:53 GMT
In general, "fail fast" approach is a good thing, but in case of
equals() it's not a normal practice to throw exception when the type is
different for one simple reason (which is quite opposite to what you
said) -- to let mixed collections exist. That is when you put integers,
strings and other objects in the same set, lists et cetera. For the
purpose you describe there were Generics added in Java 5.
By the way, you might wish to have a look at Comparator interface which
has ClassCastException throwing in its contract for compare(Object,
Object) method.
So, in my mind, Object.equals(Object) should only tell true/false,
Comparator.compare(Object, Object) should throw exception when type is
unexpected. Certainly, it's a guideline I'm following myself and you
could have something different. Thanks to Java's flexibility we have
options.