ClassCastException: [LResultImpl; incompatible with IResult
During unit test, the application throws exception that looks like: ClassCastException: [LResultImpl; incompatible with IResult.
At first, I am really curious, ResultImpl implements the interface IResult, why it can't cast ResultImpl object to IResult.
At last, we figured out that the [L means an array. it can not convert the array of ResultImpl to IResult.
This makes sense.
To verify:
public class ClassCast
{
public static void main(String[] args)
{
// ResultImpl elment1 = new ResultImpl();
IResult[] impl2 = new ResultImpl[] { new ResultImpl() };
Object o = impl2;
IResult result = (IResult) o;
}
}
interface IResult
{
}
class ResultImpl implements IResult
{
}
Result:
Exception in thread "main" java.lang.ClassCastException: [LResultImpl; incompatible with IResult
at ClassCast.main(ClassCast.java:10)