• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare: are two objects from the same class

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks rob, got it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic