# [java] java compare two Object

This topic is 5393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

so, im doing an assignments and i've run into a block (yeah, it's been a very long time since i've done any actual coding). I'm unsure as to how to compare two Object data types e.g. as is
Object a=data.returnObj();
Object b=data.returnNextObj();
int result=compare(a,b);

Obviously the code above does not work. I had a look at http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html for the "compare" method, but i'm getting compile errors; obviously I'm using it wrong. Does anybody have any suggestions as to how to compare to Object types?

##### Share on other sites
1. Never, ever, ever say "I'm getting compiler errors" and not say what they are. If you want help from people, don't expect them to be psychic.

2. What are you trying to do? The stuff about Comparator's is all about an interface that YOU have to implement - it's not magic code that somehow psychically works out what YOU mean when you ask it to "compare" two objects - you have to write the source that does that!

My guess is that what you really want is...

3. Object.equals( Object ): boolean

This tests if two objects are "the same". Read the java docs for waht "the same" means (note: it is up to the author of the class to override that method and implement it more precisely for objects of that class. For instance, you usually do something like:

public boolean equals( Object otherThing )
{
// cast otherThing to an instance of my class called "other"
// (return false if it fails)

// check that all my fields/variables have the same value as
// their counterparts in other
// (return false if any are different)

return true;
}

If the author forgets to do that, you wind up with the default implementation from Object.

redmilamber

##### Share on other sites
Its not working because you're trying to call a method without referring to the class its in.

What you want to do is like this

Object a=data.returnObj();Object b=data.returnNextObj();if(a.equals(b)) { ... }

If the two objects are the same then the if block will execute.

##### Share on other sites
what are these objects supposed to be? Dammit, I hate java's inherently unsafe types.

##### Share on other sites
the objects can be integers.

##### Share on other sites
Quote:
 Original post by yves032784the objects can be integers.

they "can" be integers? Is that to suggest that they could be something else, as well? We need to know, specifically, because comparing to Objects is pretty much a worthless pursuit, mostly a check to see if two seperate references are references to the same object.

There are many different ways to check for equality, we need to know WHAT you are comparing.

Java sucks like that.

##### Share on other sites
Quote:
 Original post by capn_midnightDammit, I hate java's inherently unsafe types.

I doesn't have any :D. Well, at least it didn't until java 5 came along (boo, hiss).

I guess you mean "I hate common-rooted type systems"? Perhaps you prefer inferred type systems ? (like ML etc)

Hmm. I wish they hadn't called the base class for all classes "Object", though - that was just stupid! You wouldn't believe how many students THAT one has confused :(.

##### Share on other sites
Quote:
Original post by capn_midnight
Quote:
 Original post by yves032784the objects can be integers.

they "can" be integers? Is that to suggest that they could be something else, as well? We need to know, specifically, because comparing to Objects is pretty much a worthless pursuit, mostly a check to see if two seperate references are references to the same object.

There are many different ways to check for equality, we need to know WHAT you are comparing.

Java sucks like that.

Well since i am using Object, i REALLY want to be able to use the compare method (see the link i gave in the first post). My program will be also comparing strings and other types. Thanks for the 'equals' method, but that's not what i need, i need something that will perform like 'compare'

##### Share on other sites
Quote:
 Original post by Anonymous PosterHmm. I wish they hadn't called the base class for all classes "Object", though - that was just stupid! You wouldn't believe how many students THAT one has confused :(.

O_O

What else could you call it?

And now to address the OP:

Quote:
 Well since i am using Object, i REALLY want to be able to use the compare method (see the link i gave in the first post). My program will be also comparing strings and other types. Thanks for the 'equals' method, but that's not what i need, i need something that will perform like 'compare'

A Comparator is an interface. You must implement it. That is, you make another class, which "implements Comparator", and provides the named methods. This new class' purpose in life is to... compare pairs of other objects:

class MyFoo implements Comparator {  public int compare (Object a, Object b) {    // return -1, 0 or 1 depending on how 'a' compares to 'b'.    // You have to write code here which makes that determination,    // by examining public data (bad) or calling public methods    // (better) on the input objects - and you will probably have    // to do a lot of checking to figure out what *kind* of Objects    // you have.  }}

This is a rather advanced concept and it is probably not the thing you are really looking for. Comparators are more useful as things to pass to a sorting algorithm (say); they are basically supposed to be like comparison functors in C++ (like std::greater_than).

What you are probably thinking of is the compareTo(Object obj) method in interface Comparable (not Comparator). Again, you have to implement this, but now you are implementing it directly in the class you want to compare:

class MyBar implements Comparable {  public int compareTo (Object o) {    // Now you write code to determine how 'this' compares to 'o'.    // Notice that everything in the current object is accessible.    // You still have to check the type of o, though. A common approach is:    MyBar other = (MyBar)o;    // If that fails, it will throw ClassCastException, but that    // is ok because that's exactly what you are supposed to do    // if you are given a non-comparable object.    // Now, compare first by the 'x' data member, then by 'y'    if (x != other.x) return x - other.x;    // If there is a difference, the subtraction will indicate    // which is bigger.    // Otherwise they are equal, so check y    return y - other.y; // possibly 0.  }}

##### Share on other sites
Quote:
 Original post by yves032784Well since i am using Object, i REALLY want to be able to use the compare method (see the link i gave in the first post). My program will be also comparing strings and other types. Thanks for the 'equals' method, but that's not what i need, i need something that will perform like 'compare'

You still haven't told us what the compiler errors were.

There is no such thing as "somethign that will perform like compare" because, by definition, it's impossible to do any compare beyond "are these the same object in memory?" for a random Object - it is up to YOU to invent an algorithm that makes sense for YOUR particular classes/objects and implement it.

?

• 18
• 29
• 11
• 21
• 16