• 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

getClass and instanceOf

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between the instanceof and getclass, these two are same or not ?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are very different.

The instanceof operator tests whether its left-hand operand can be cast to the class named on its right and returns a boolean. If the left operand is an object of a subclass of the named class, it still returns true, and if you write null instanceof Foo, you always get false.
I have never seen getclass, but the Object#getClass() method returns the Class<?> object which that object was created from. If you call it on any non-null reference type you get a Class<?> object, but if you call it on a null reference you suffer a NullPointerException.
 
Sheriff
Posts: 22781
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
instanceof is an operator, getClass is a method.

If you refer to the use of both in equals implementations, the difference is allowing sub types like Campbell said.

Let's take java.util.Date as an example. If the equals method is written like this then for any instance of, let's say, java.sql.Timestamp with the same return for getTime(), equals will return true.

If equals is written like this Now an instance of java.sql.Timestamp will never be equal to an instance of java.util.Date because their classes are different.

Now you may want to know why the second mechanism exists. The reason is simple: to keep to the contract of Object.equals. It says that if an object X equals another object Y, then Y must also equal X. Another example when this can be broken using instanceof:
Assume you have an instance of Point called p and an instance of ColorPoint called cp. Both have values for x and y of 0. Now p.equals(cp) because cp is also a Point with the same x and y values. However, cp.equals(p) is false since p is not a ColorPoint. If you would have used getClass() then p.equals(cp) would also return false since they have different classes.

In short, only use instanceof in the equals method if you cannot override it. Either make the method or the class final. Otherwise getClass() is the only way to go to guarantee that the contract for Object.equals is fulfilled.
 
reply
    Bookmark Topic Watch Topic
  • New Topic