This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes compare: are two objects from the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "compare: are two objects from the same class" Watch "compare: are two objects from the same class" New topic
Author

compare: are two objects from the same class

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I want to compare classes:

What is better?

This:

way 1: // compare, if the value of s is the same as User.class



or that

way 2: // compare, if the reference of s is the same as User.class



I only want to compare, that two objects are from the same class.

So in my case, way 2 would be better(and faster),
as it do not need the equals-Methods from the class. Am I right?
Jagdish Hatagale
Ranch Hand

Joined: Apr 07, 2010
Posts: 33
You must have to use equals method you are comparing object,
becose == only check the sequence of stream not the type of objcet and equals check the both type of object and stream sequence .
or while you just want to compare object from same class or not
why cant you use instanceOf method to ceack object.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello,

I do NOT want to compare objects (in this case, equals is the only way).

I only want to compare, if two objects are from the same class!

So for my opinion, this



is the best choice, or?


So you say:

I should use this:



instead of this:



What about inheritance. Why should I use instanceOf when I can use "==" ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Have you read the API documentation for the Class<T> class? Have you seen whether its equals() method is overridden or inherited from Object? If it is inherited from Object, how does Object implement the equals() method?

And instanceof tests whether the object reference can be cast to the class type of its right operand. So a subclass object can always be cast to its superclass
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580

Have you seen whether its equals() method is overridden or inherited from Object?


The java.lang.Class does not overwrite equals-method.

So this

o.getClass() == f.getClass()

is correct.

I was a little confused, as

this



and this



both returned true.

So I better to use this:



I do not understand, in which cases this makes sense:


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

They are the same; Object.equals only uses == and Class inherits this implementation.

nimo frey wrote:

That won't compile - Class has no method called instanceOf. You probably meant either "s.getClazz() instanceof User" which will return false since s.getClazz() returns a Class object that will never be an instance of class User, or "s.getClazz().isInstance(User.class)" which will always only return true if s.getClazz() returns Class.class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks rob, got it!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compare: are two objects from the same class
 
Similar Threads
when are two objects equal?
compare two classes with .getSimpleName()
Equal(),==
questions on Generic
a question about equals() method?