Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instanceof use for multiple types

I am writing a TypeChecker for MiniJava and ExpOp needs to check if both of the entered expressions are of Integer to use plus, minus, times.

How can I write a line of code inside an if statement that includes both expressions and checks if both of them are instances of (instanceof) Integer?

This is what I have now:

n.e1.accept(this) n.e2.accept(this) instanceof Integer

Appreciate your help.

like image 592
Limeran Avatar asked Apr 12 '13 21:04

Limeran


People also ask

Does Instanceof check for subclasses?

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.

Does Instanceof work for interfaces?

instanceof can be used to test if an object is a direct or descended instance of a given class. instanceof can also be used with interfaces even though interfaces can't be instantiated like classes.

What can I use instead of Instanceof in Java?

Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism".

What is the purpose of Instanceof operator in 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 .

Can I use Instanceof?

instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false. It's also known as a type comparison operator because it compares the instance with the type. Before casting an unknown object, the instanceof check should always be used.


2 Answers

You can make a utility function that uses the reflection counterpart of instanceof, Class.isInstance():

public static boolean allInstanceOf(Class<?> cls, Object... objs) {
    for (Object o : objs) {
        if (!cls.isInstance(o)) {
            return false;
        }
    }
    return true;
}

You use it like this:

allInstanceOf(String.class, "aaa", "bbb"); // => true
allInstanceOf(String.class, "aaa", 123); // => false
like image 196
millimoose Avatar answered Sep 21 '22 01:09

millimoose


instanceof is a binary operator: it can only have two operands.

The best solution for your problem is Java's boolean AND operator: &&.

It can be used to evaluate two boolean expressions: <boolean_exp1> && <boolean_exp2>. Will return true if and only if both are true at the time of the evaluation.

if (n.e1.accept(this) instanceof Integer &&
    n.e2.accept(this) instanceof Integer) {
    ...
}

That being said, another possible solution is to cast them both inside a try/catch block, and when one of them is not an Integer a ClassCastException will be thrown.

try {
   Integer i1 = (Integer) n.e1.accept(this);
   Integer i2 = (Integer) n.e2.accept(this);
} catch (ClassCastException e) {
   // code reached when one of them is not Integer
}

But this is not recommended as it is a known anti-pattern called Programming By Exception.

We can show you a thousand ways (creating methods, creating classes and using polymorphism) you can do that with one line, but none of them will be better or clearer than using the && operator. Anything other than that will make you code more confusing and less maintainable. You don't want that, do you?

like image 40
acdcjunior Avatar answered Sep 20 '22 01:09

acdcjunior