File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes compare object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare object" Watch "compare object" New topic

compare object

Stee Munche

Joined: Oct 07, 2005
Posts: 4
I wanted to know if there exist already a method to compare objects. For example I have to compare two sub class objects. I want to know if they are of the same type(i.e an object student is not the same as an object address) and if they are it must return me true.
I hope someone will be able to help me.

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
If you want to compare two objects and have the comparison look at your specifically specified fields, then you have to write the code to do that.

To know if an Object is of a specific type, use the instanceof operator.
marc weber

Joined: Aug 31, 2004
Posts: 11343

It all depends on how the classes were written. Specifically, whether the classes themselves implement Comparable (so they can be compared to each other), or whether there exits a Comparator that will take instances of these classes as arguments.

If all you're concerned about is whether instances are "equal" and not concerned about any ordering relations if they're not, then the equals method should suffice, although this also requires that the method is overridden to give meaningful results. (And this would also limit the types of Collections that these instances could be placed in.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. We take this policy rather seriously. Thanks for your cooperation.

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Do you want to compare the objects or their types? These are two different ideas. To compare two objects, you typically need to implement Comparable or Comparator or override the equals() method depending on what kind of comparison you need.

If you want to determine the type of an object, you can use the instanceof operator. If you have two objects and whant to compare their types, you can use the getClass() method and compare the Class objects that are returned from each one. I think Class overrides equals. However, I highly doubt that this is what you want to do. I suspect the suggestions in my first paragraph are closer to what you really need to know.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: compare object
jQuery in Action, 3rd edition