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.
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.
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.
Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism".
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 .
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.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With