Re: Java type-casting -- Q1
grz01 wrote:
On 25 Sep, 23:40, grz01 <gr...@spray.se> wrote:
List<String> sList = new ArrayList<String>();
List<Object> oList = (List<Object>)sList;
the last assignment give me an error:
Cannot cast from List<String> to List<Object>
I discovered, however, that this works:
List<String> sList = new ArrayList<String>();
List<?> oList = sList;
What's the significant difference between the two?
Assume casting to List<Object> worked, here is what happens:
List<String> sList = new ArrayList<String>();
List<Object> oList = (List<Object>)oList;
oList.add(new Integer(3)); // compiles fine because oList takes Objects
String r = sList.get(0); // Compiles fine, but causes
//a ClassCastException.
Any good articles on these issues you can point me to?
In your second example:
List<String> sList = new ArrayList<String>();
sList.add("Hello");
List<?> oList = sList;
Object o = oList.get(); // Compiles fine, o = "Hello"
oList.add(new Integer(3)); // Fails at compile time
One general rule that I've found is:
When you only read from a structure, declare it <? super Type>.
When you only write to a structure, declare it <? extends Type>.
When you read/write to a structure, declare it <Type>.
For example:
public void processNumbers(List<? extends Number> numberList);
public void createRandomIntegers(Collection<? super Integer> target);
public <T> void reverse(List<T> list);
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>