Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between raw types and <?> in Generics

It may be asked somewhere but I could not find it.

Please tell me the exact difference between:

ArrayList list = new ArrayList();

and

ArrayList<?> list = new ArrayList();

I cannot figure out what is the exact difference between these two.

Thanks...

like image 997
Chintan Patel Avatar asked Sep 21 '13 02:09

Chintan Patel


People also ask

What is generic raw type?

A raw type is the name of a generic class or interface without any type arguments. For example, given the generic Box class: public class Box<T> { public void set(T t) { /* ... */ } // ... }

What is raw or not type?

Raw types refer to using a generic type without specifying a type parameter. For example: A list is a raw type, while List<String> is a parameterized type.

Is ArrayList a raw type?

Definition: The raw type is the generic type without any arguments. For example, ArrayList<String> and ArrayList<JFrame> are generic types, while ArrayList is a raw type.


2 Answers

ArrayList<?> simply means "any type." In other words, any type of ArrayList can be assigned to such variable. That could be ArrayList<Integers>, ArrayList<JButton> or anything else. Using the wildcard alone, without the keyword super (followed by a type), means that you cannot ADD anything to the list defined as ArrayList<?>. ArrayList alone however, means the old style type-less ArrayList which you can do all sorts of operations including add.

List<?> list;
List<Integer> ints = new ArrayList<Integer>();
List<Integer> strings = new ArrayList<Integer>();
list = ints; // valid
list = strings; // valid
list.add("new"); // compile error

UPDATE:

Suppose I have following method:

void insert(List list) {
   // loop through list, do whatever you like
   list.add("my string"); // dangerous operation 
}

Now if I call insert(ints) compiler will generate a warning but will not prevent me of adding a String to a list of integers. Changing method to following:

void insert(List<?> list) {
   // loop through list, do whatever you like
   list.add("my string"); // compiler error on this dangerous operation
}

would prevent me of doing such an operation.

like image 183
Ean V Avatar answered Oct 26 '22 22:10

Ean V


ArrayList list = new ArrayList();

We are declaring an array list that can accept any type of objects.

For example:

list.add(new Dog());
list.add(new Person());
list.add("Test");

For ArrayList<?> list = new ArrayList();

We are declaring an array list using generics that can accept any object using the wild card ?

The catch here is that we cannot add elements to that array list.

This code will not even compile:

ArrayList<?> list = new ArrayList();
list.add("test");

Update:

I think the only purpose of the ? wild card in generics is to be coupled by the extends keyword.

ArrayList<? extends Animal> list = new ArrayList<Dog>();

In that case we add any object to list that extends the Animal object

or to be passed to as a parameter to a method.

public void foo(List<?> list) { }

In that case the method foo cannot add objects to the parameter list

like image 20
Rami Avatar answered Oct 26 '22 23:10

Rami