?
Solved

compare between type of two objects

Posted on 2002-05-29
6
Medium Priority
?
225 Views
Last Modified: 2010-03-31
I want to compare between type of two objects:
Which method is better and why?

class MyClass
{
  boolean equals(Object obj)
  {
    return (obj instanceof MyClass);
  }
//or:
  boolean equals(Object obj)
  {
    return (obj.getClass().equals(this.getClass()))
  }
}
0
Comment
Question by:s_lavie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
ID: 7042344
I would go with instanceof. 1) It is substantially faster -- 5x faster on JDK 1.3 -- as it is evaluated entirely within the interpreter as opposed to the getClass method which requires three additional method calls. 2) The instanceof operator inherently deals with null references whereas you would have to do an explicit null check on the supplied argument to avoid throwing NullPointerException using getClass.

Jim
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7042362
The former - it produces fewer instructions.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7044597
Hi,

Method to choose depends on what you want to achieve. Fist method will return true if obj's class is subclass of MyClass, while second checks for precise class equality.

I'm afraid there is no way to check for class equality using instanceof operator. But if you want comparison to return true for subclasses, than instanceof is a good choice.

You might want to consider another subclassing issue. If you make subclass of MyClass, what your equals() would mean? Would you like to rewrite equals() for each subclass? Or rewrite your first approach to reflection:

boolean equals(Object obj)
 {
   return getClass().isInstance(obj);
 }

Or choose second approach?

Regards,
Igor Bazarny,
Brainbench MVP for Java 1

0
 
LVL 35

Expert Comment

by:girionis
ID: 8813228
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to jim_cakalic@idg

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 8813727
OK. Thanks. :-)
Jim
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question