Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java isInstance vs instanceOf operator

Tags:

The whole generics thing is kinda throwing me for a loop, and more so the RTT.

Specificis? Ah well here's the gist:

enum QueryHelper {   query1,   query2;   static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {     if (expectedReturn.isInstance (SomeRelatedClass.class))       return query1;     else       return query2;   } } 

and then I would call it like so:

... QueryHelper helper = QueryHelper.getQueryHelper(SomeRelatedClass.class); ... 

This is so that I can really flexibly assign the query return type in the actual helper. It does some casting and object creation. What I am seeing is that there is no match, should I be doing this some other way? Or is the whole idea just bad?

And the real heart of this is that I don't understand the difference between class.isInstance and the instanceOf operator? Should I be using the latter?

like image 360
rybit Avatar asked Nov 10 '10 00:11

rybit


People also ask

What is the difference between using the getClass method and using the Instanceof operator?

Coming to the point, the key difference between them is that getClass() only returns true if the object is actually an instance of the specified class but an instanceof operator can return true even if the object is a subclass of a specified class or interface in Java.

Is Instanceof an operator in java?

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.

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 .

IS NULL check needed before calling Instanceof in java?

No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null . The Java 11 Language Specification expresses this concisely in section 15.20.


2 Answers

This is so that I can really flexibly assign the query return type in the actual helper.

There is nothing flexible about the return type of this method

static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {     if (expectedReturn.isInstance (SomeRelatedClass.class))       return query1;     else       return query2; } 

It will always return an instance of QueryHelper. If you want the return type to be flexible you would need to define it as something like:

static <T> T getQueryHelper (Class<T> expectedReturn) { } 

Now the return type is flexible, because it will depend on the type of the argument

And the real heart of this is that I don't understand the difference between class.isInstance and the instanceOf operator?

The difference is that instanceof does a type-check that is fixed at compile-time, for example:

static boolean isInstance(Object myVar) {     return (myVar instanceof Foo); } 

will always check that myVar is an instance of Foo, whereas

static <T> boolean isInstance(Object myVar, Class<T> expectedType) {     return expectedType.isInstance(myVar); } 

will check that myVar is an instance of expectedType, but expectedType can be a different type each time the method is called

like image 74
Dónal Avatar answered Sep 23 '22 19:09

Dónal


Class.isInstance() doesn't work like your code expects. It tests whether the object you pass to it is an instance of the class. In you code:

expectedReturn.isInstance(SomeRelatedClass.class) 

The object you're passing is a Class object. Try this instead, which returns true:

Class.class.isInstance(SomeRelatedClass.class); 

What you're probably looking for is Class.isAssignableFrom(), e.g.:

Object.class.isAssignableFrom(Class.class); 

Means you can do this:

Class klass = ...; Object o = klass; 
like image 31
vanza Avatar answered Sep 23 '22 19:09

vanza