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
Solved

compare between type of two objects

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 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