According to the JLS, Java SE 7 edition, §15.20.2 (Type Comparison Operator instanceof
):
If a cast of the RelationalExpression to the ReferenceType would be rejected as a
compile-time error, then the instanceof
relational expression likewise produces
a compile-time error. In such a situation, the result of the instanceof
expression
could never be true.
And §15.16 (Cast Expressions) states:
It is a compile-time error if the compile-time type of the operand may never be
cast to the type specified by the cast operator according to the rules of casting
conversion (§5.5).
Finally, §5.5.1 (Reference Type Casting) states:
Given a compile-time reference type S (source) and a compile-time reference type
T (target), a casting conversion exists from S to T if no compile-time errors occur
due to the following rules.
[...]
If S is an array type SC[], that is, an array of components of type SC:
- If T is an interface type, then a compile-time error occurs unless T is the type
java.io.Serializable
or the type Cloneable
(the only interfaces implemented
by arrays).
Therefore, Java requires that your test to see if an array type is an instance of java.lang.Iterable
results in a compile-time error.
If you want to try and make it work (always return false
) anyway, you can cast the array to Object
first, like so:
System.out.println((((Object)array) instanceof Iterable));