[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

compare between type of two objects

Posted on 2002-05-29
6
Medium Priority
?
229 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

656 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