• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compare: are two objects from the same class

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "==" ?

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay thanks rob, got it!
reply
    Bookmark Topic Watch Topic
  • New Topic