Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between an unbound wildcard and a raw type

I was reading about generics and I did not understand the need for unbound wildcards and how it differs from raw type. I read this question but still did not get it clearly. In the Java tutorial page for unbound wildcard I got below two points and I did not understood first point:

  • If you are writing a method that can be implemented using functionality provided in the Object class.
  • 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<T> do not depend on T.

Can someone please explain the difference between unbound wildcard and raw type in layman language.

How does List<?> differ from List<Object>?

like image 276
Sandeep Kumar Avatar asked Jan 09 '13 16:01

Sandeep Kumar


People also ask

In which situations would you use unbounded wildcards?

There are two scenarios where an unbounded wildcard is a useful approach: If you are writing a method that can be implemented using functionality provided in the Object class. When the code is using methods in the generic class that don't depend on the type parameter.

What does raw type mean?

A raw type is the name of a generic class or interface without any type arguments.

What are the types of wildcard?

There are three types of wildcards in Java which are Upper Bounded wildcards, Lower Bounded, and Unbounded wildcards.

What are bounded and unbounded wildcards in generics?

both bounded and unbounded wildcards provide a lot of flexibility on API design especially because Generics is not covariant and List<String> can not be used in place of List<Object>. Bounded wildcards allow you to write methods that can operate on Collection of Type as well as Collection of Type subclasses.


2 Answers

How List<?> differs from List<Object>

The main difference is that the first line compiles but the second does not:

List<?> list = new ArrayList<String> (); List<Object> list = new ArrayList<String> (); 

However, because you don't know what the generic type of List<?> is, you can't use its parameterized methods:

List<?> list = new ArrayList<String> (); list.add("aString"); //does not compile - we don't know it is a List<String> list.clear(); //this is fine, does not depend on the generic parameter type 

As for the difference with raw types (no generics), the code below compiles and runs fine:

List list = new ArrayList<String> (); list.add("aString"); list.add(10); 
like image 105
assylias Avatar answered Oct 07 '22 15:10

assylias


How List<?> differs from List<Object>?

    List<Object> l1 = new ArrayList();     List<?> l2 = new ArrayList();     l1.add("Object");     //l2.add("Object");   incorrect     l2.add(null); 

You can only add null-value to the List<?>

like image 29
Anton Kasianchuk Avatar answered Oct 07 '22 17:10

Anton Kasianchuk