0

I came to know the Unbounded Wild card but I didn't understand usage of it in realworld. In Below program using and with out using this Unbounded Wildcard will be same at compile time.Then why we need Unbounded wild cards ?Please help to understand.

public class UnboundedWildCards {
    public static void main(String args[])
    {
        List<Integer> intLst = new ArrayList<Integer>();
        intLst.add(1);
        intLst.add(11);     

        List<Double> doubleLst = new ArrayList<Double>();
        doubleLst.add(1.1);
        doubleLst.add(11.11);       

        List lst = new ArrayList();
        lst.add(new Object());
        lst.add(new Double(2.3));
        lst.add(new Integer(2));

        print(intLst);
        print(doubleLst);
        print(lst);

        unBoundprint(intLst);
        unBoundprint(doubleLst);
        unBoundprint(lst);
    }
    public static void print(List lst)
    {
        for(Object o : lst)
            System.out.println(o);
    }
    public static void unBoundprint(List<?> lst)
    {
        for(Object o : lst)
            System.out.println(o);
    }
}
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • THIS ONE IS VERY USEFUL http://stackoverflow.com/questions/14242174/difference-between-an-unbound-wildcard-and-a-raw-type?rq=1 – sunleo Feb 05 '14 at 12:54

3 Answers3

1

There are two scenarios where an unbounded wildcard is a useful approach:

  1. If you are writing a method that can be implemented using functionality provided in the Object class.
  2. When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size or List.clear. In fact, Class is so often used because most of the methods in Class do not depend on T.

with examples http://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html

A Paul
  • 8,113
  • 3
  • 31
  • 61
  • What exactly do you want to know. What is your confusion from that read. – A Paul Feb 05 '14 at 12:39
  • Another good post for you. http://stackoverflow.com/questions/15568460/generics-java-unbounded-wildcard – A Paul Feb 05 '14 at 12:41
  • 1
    Another case: If you have to use code from the non-generic time period (e.g. java 1.4). `List> list = oldShittyCode.getList();` – Hannes Feb 05 '14 at 12:53
1

Are you asking why do we need the notation

List<?> 

when the simpler

List 

is equivalent?

Using the wildcard is more explicit but the simpler syntax is required for backwards compatibility with pre-generics code. So you can use either interchangeably but there is also a third way of declaring this list which is also equivalent.

List<? extends Object> list;

This list will contain only objects of type Object. Since everything in Java extends from Object this is basically the same as saying unbounded.

Personally of the three I would probably use the first one because it is concise while still explicitly stating what the List should contain. If you just declared it as a List without a generic argument then it could be mistaken for a pre-generics list.

When would you ever want to use this? From experience I have only used this kind of unbounded list with legacy code. I was always taught to only put objects of one common type into a list (like a list of customers etc). However, people don't always write nice code and at times I've had to deal with lists containing different types. In these cases it was hard to move to a proper genericised list so I had to make do with an unbounded list.

It is possible that you may have a legitimate cause for an unbounded list. It depends what you want to do with those objects. If once they've been added to the list you only need to call methods from the Object class (like toString) or perhaps the List interface (like size), then perhaps you have a genuine use case for an unbounded list.

Ben Thurley
  • 6,943
  • 4
  • 31
  • 54
0

At first it is important to understand that List<Object> and List<?> are not same. List<Object> is not super class of List<Anything that extends object> but List<?> or List<? extends Object> is super class of List<Anything that extends object>.

After keeping above point in mind go through printList example from this oracle's link https://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html

RakeshRaj
  • 69
  • 9