Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Class.isInstance() & when to use instanceof operator? [duplicate]

Tags:

java

Possible Duplicate:
Java isInstance vs instanceOf operator

When to use Class.isInstance() and when to use instanceof operator?

Java is providing two option for checking assignment compatibility. Which to use when?

like image 548
Jyotirup Avatar asked Jan 01 '12 07:01

Jyotirup


People also ask

What is the difference between class isInstance and instance of operator?

The instanceof operator and isInstance() method both are used for checking the class of the object. But the main difference comes when we want to check the class of objects dynamically then isInstance() method will work. There is no way we can do this by instanceof operator.

What is isInstance function in Python?

Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.

How do you check if an object belongs to a certain class?

The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.

How do you check if an object is an instance of another object Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .


2 Answers

For instanceof you need to know the exact class at compile time.

if (foo instanceof ThisClassIKnowRightNow)    ... 

For isInstance the class is decided at run time. (late binding) e.g.

if (someObject.getClass().isInstance(foo))    ... 
like image 118
user949300 Avatar answered Sep 19 '22 19:09

user949300


I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific way):

This method is the dynamic equivalent of the Java language instanceof operator.

I take that to mean that isInstance() is primarily intended for use in code dealing with type reflection at runtime. In particular, I would say that it exists to handle cases where you might not know in advance the type(s) of class(es) that you want to check for membership of in advance (rare though those cases probably are).

For instance, you can use it to write a method that checks to see if two arbitrarily typed objects are assignment-compatible, like:

public boolean areObjectsAssignable(Object left, Object right) {     return left.getClass().isInstance(right); }  

In general, I'd say that using instanceof should be preferred whenever you know the kind of class you want to check against in advance. In those very rare cases where you do not, use isInstance() instead.

like image 39
aroth Avatar answered Sep 22 '22 19:09

aroth