Generic types : wildcards vs variables of raw types

generic-collections, java, oop

Solution

`<?>` doesn't allow you to add objects in list. See the program below. It is specific type of list we have passed to method `<?>`. Specific means, list was created with specific type and passed to `<?>` method `listAll`. Don't confuse with word `specific`. Specific can be any normal object, like, Dog, Tiger, String, Object, HashMap, File, Integer, Long.... and the list is endless. `JLS` forces `<?>` method for not to perform add any `irrelevant objects` in called `<?>` method once you have defined (defined in calling method not in `called-listAll` ) list containing `specific type` of object. It is like `<?>` saying "don't touch me".

public static void listAll(LinkedList list) 
{
    list.add(new String());  //works fine
    for(Object obj : list)
            System.out.println(obj);

}
public static void listAll(LinkedList<?> list) 
{
     list.add(new String());  //compile time error. Only 'null' is allowed.
     for(Object obj : list)
          System.out.println(obj);
}

Now let's look at the different scenario. What will happen when we declare specific type like, Dog, Tiger, Object, String ..... anything. Let's change the method to `specific type`.

public static void listAll(LinkedList<String> list)// It is now specific type, 'String'
{
    list.add(new String());//works fine. Compile time it knows that 'list' has 'String'
    for(Object obj : list)
         System.out.println(obj);
}

Problem

Consider following methods: ``` public static void listAll(LinkedList list) { for(Object obj : list) System.out.println(obj); } ``` and ``` public static void listAll(LinkedList<?> list) { for(Object obj : list) System.out.println(obj); } ``` What is the difference between these two methods? If there is no difference, why we should use the second one?

Original source

Related problems