Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

What is better way to compare class of two objects

I can compare two different objects to know whether they belong to same class or different using both == operator and equals method also. But which one is the better approach and how? Have a look on both approach that i followed. What is better way to compare class of two objects

 //r2 and r3 are two different objects    
//first approach   Using == operator
    boolean firstApproach = ((Object)(r2.getClass()) ==((Object)r3.getClass()));

//second approach   Using equals method to compare
            boolean secondApproach = ((Object)(r2.getClass())).equals(((Object)r3.getClass()));
            System.out.println("...firstApproach ..."+firstApproach +"...secondway.."+secondway );

Upvotes: 0

Views: 351

Answers (6)

Stephen C
Stephen C

Reputation: 718718

From a semantic perspective, there is no difference between using == or equals to compare two Class objects. They give the same answer.

If there is any difference it is in the code's readability. While Java experts should know that you can safely use == to compare classes, non-experts are less likely to know this and use of == is likely to ring alarm bells for them. So arguably using equals makes your code more readable ... for the next guy, if not for you. But this is a marginal call.


If you are concerned about readability, then you should get rid of the unnecessary (Object) type-casts. They serve no purpose ... apart from making your code harder to read.

Upvotes: 1

Mr. Elusive
Mr. Elusive

Reputation: 496

Your first approach has two main limitations: 1) if you have to implement this logic in multiple places in your code and also if you wish to change the criteria by which to check whether two objects are equal, you have to change your code in many different places 2) behind the scenes, Java often wants to compare two objects, but it doesn’t know by default how to determine equality, so you miss out on that if you don’t follow the second approach.

So, I feel the second approach is better.

Upvotes: 0

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

if you want to know if its the same use .equals but more likely you might want to use instanceof to determinate if an object is in an instance of a class.

Upvotes: 0

Lukas Eder
Lukas Eder

Reputation: 220762

java.lang.Class inherits its equals() implementation from java.lang.Object:

public boolean equals(Object obj) {
  return (this == obj);
}

So it doesn't matter which way you compare classes. Also, class loaders are irrelevant as the behaviour will stay the same.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533472

Why not?

boolean sameClasses = r2.getClass() == r3.getClass();

Upvotes: 4

korifey
korifey

Reputation: 3509

Simply r2.getClass() == r3.getClass() without casts. If classes loaded by same classloader (I suppose in your case it's like that) then no problems with identity comparation.

Upvotes: 1

Related Questions