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 interfaceSerializable
and Cloneable
when using operation instanceof
. Can someone tell me why?
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 theinstanceof
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 typeCloneable
(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));
You are confusing suitability of an object for use in a foreach loop, and the Iterable
interface, which arrays do not implement.
Both arrays and Iterable are supported with the foreach syntax.
That doesn't mean that arrays are Iterable.
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