File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 38791
    
  23
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: 19693
    
  20

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare: are two objects from the same class