Just come across with this problem:
List<DataNode> a1 = new ArrayList<DataNode>();
List<Tree> b1 = a1; // compile error: incompatible type
Where the type DataNode is a subtype of Tree.
public class DataNode implements Tree
To my surprise, this works for array:
DataNode[] a2 = new DataNode[0];
Tree[] b2 = a2; // this is okay
This likes a bit strange. Can anyone give an explanation on this?
What you’re seeing in the second case is array covariance. It’s a bad thing IMO, which makes assignments within the array unsafe – they can fail at execution time, despite being fine at compile time.
In the first case, imagine that the code did compile, and was followed by:
What would you expect to happen?
You can do this:
… because then you can only fetch things from
b1, and they’re guaranteed to be compatible withTree. You can’t callb1.add(...)precisely because the compiler won’t know whether it’s safe or not.Have a look at this section of Angelika Langer’s Java Generics FAQ for more information.