Solved

compare between type of two objects

Posted on 2002-05-29
6
183 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
6 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now