File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes getClass and instanceOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "getClass and instanceOf" Watch "getClass and instanceOf" New topic

getClass and instanceOf

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
What is the difference between the instanceof and getclass, these two are same or not ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: getClass and instanceOf
It's not a secret anymore!