Question: 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...

Answer: 
```
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**
There is no difference in the behaviour.

The real difference is in the way that the compiler treats them. In the first case, you are telling the compiler "treat this as a *raw* type" and don't try to do any generic static typing. In the second case, you are saying "treat this as a generic type" ... but that the actual type parameter is "some type that we want to avoid specifying here".

And note that the `<?>` wildcard syntax cannot be used where a definite type is required.

@SJuan76 commented:

> 
> "(I guess they didn't add the feature just to get some code to produce compilation errors)"
> 
> 
> 

Well, actually you could say that that *is* that they did. Or more precisely, they left the old form so that old (pre-Java 5) code would continue to compile *without* compilation errors using a Java 5+ compiler.