Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
SCJP 5.0
Note: There is one exception: You can always add "null". For example the following will compile fine and print 1:At compile time, you cannot add to a collection that has <? extends ...>
SCJP 5 (98%) - SCBCD 5 (98%)
SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Originally posted by Keith Nagle:
At compile time, you cannot add to a collection that has
<? extends ...> for the simple reason that you could be adding something that could upset your collection.
Just remember that if you want to add something to a collection, the following are legitimate:
Best regards.
[ July 11, 2008: Message edited by: Keith Nagle ]
Originally posted by Frank Zito:
I quickly tried this out and compiler was complaining:
$ javac GenericsPractice.java
GenericsPractice.java:25: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());
^
GenericsPractice.java:26: cannot find symbol
symbol : method add(java.lang.Object)
location: interface java.util.List<capture of ? super Dog>
l.add(new Object());
^
2 errors
I don't understand why? The code made perfect sense to me!
Any else have this bizarre problem??
It's as if the compiler cannot see "? super" ... this has got me worried now!
I'm using jdk 1.5.0_10
[ July 11, 2008: Message edited by: Frank Zito ]
Originally posted by Frank Zito:
I guess the code was a little misleading; probably the type of code to find in an exam!
obviously, code compiles for the more general case:
public void addAnother(List<? super Object> l)
which is equivalent as non-generic :
public void addAnother(List l)
Originally posted by Raphael Rabadan:
You said what i've said :-)
He can only add Dog's in the example, you just give the explanation for it :-)
Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Originally posted by Faber Siagian:
Oooops, it is more confusing now.
Yes i know that we can't add any element into a collection, but in the code, nothing is added into basket. I just set basket's element.
Or there's something else that i missed about collection?