Why doesn't "array instanceof Iterable" compile in Java?
arrays, compiler-errors, java
Solution
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));
Problem
``` Object[] array = new Object[]{}; System.out.println((array instanceof Serializable));//passed System.out.println((array instanceof Cloneable));//passed ``` This code compiles and runs. The output is: ``` true true ``` However, this code doesn't compile: ``` System.out.println((array instanceof Iterable));//not passed ``` The Eclipse compiler reports: Incompatible conditional operand types Object[] and Iterable I found that arrays can only be compared between interface`Serializable` and `Cloneable` when using operation `instanceof`. Can someone tell me why?