Trouble with comparing objects

Okay, this might seem sort of an odd question, but I need some help with something. I'm running a program that takes an Object, and I'm trying to compare it to a String for equality. So if I had something say like......

Object x ="+";

String y ="+";

How do I compare the two in such a way as to have them be equivalent (x==y) ?

Thanks

[419 byte] By [onewingdphoenixa] at [2007-11-26 19:34:05]
# 1

Depends what you mean by "equivalent" and "equality". If you want to compare whether two references refer to the same object then you use the "==" operator. If you want to find whether they have the same content, then you use the equals() method. Assuming the objects in question support it, of course. Which in this case they do, since they are both String objects.

Now neither == nor equals() cares what type of variable is referring to the objects. So it is irrelevant that one reference is assigned to a variable of Object type, the object in question is still a String.

DrClapa at 2007-7-9 22:07:17 > top of Java-index,Java Essentials,Java Programming...
# 2

Both of your objects are Strings with the value "+", so you can just do x.equals(y).

Now, if you had this:class Foo {

String str;

public String toString() {

return str;

}

}

you could do x.equals(foo.toString())

jverda at 2007-7-9 22:07:17 > top of Java-index,Java Essentials,Java Programming...