I am attempting to understand java generic wildcards.I came across a basic program.It appears to me that the only thing that can be added to the list s is "null". You cannot add for example
s.add(new Integer (4));
OR
s.add(new Char(a));
Before reading a lot of material and confusing my mind,I am attempting to understand where the Java designers are going with introducing wildcards in generics.From my point of view I cannot see any useful feature coming from Generic wildcards.It is possible that it appears so due to my lack of experience and since I am new to java.It would be good if people could share their thoughts on the matter.
import java.util.LinkedList;
import java.util.Iterator;
public class WildCardExampleOne {
public static void main(String[] args)
{
LinkedList<?> s = new LinkedList<Integer>();
s.add(null);
Iterator<?> x = s.iterator();
Object o = x.next();
System.out.println(o);
/* note that LinkedList<?> is NOT the same type as LinkedList<Object>
* here are some statements that wouldn't compile:
*/
// s.add(new Integer(3));
// Iterator<Object> x = s.iterator();
}
}