Solved

compare between type of two objects

Posted on 2002-05-29
6
203 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

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