unexpected type compilation error in generic classes,What maybe possible arguments for it?

i am beginning Collection Frameworks in java, I have a simple code but its make me curious about what maybe possible arguments for generic classes.

import java.util.ArrayList;
import java.util.LinkedList;

class ListExa{
public static void main(String[] args){
    ArrayList<int> names = new ArrayList<int>();



ListExa.java:6: error: unexpected type
                ArrayList<int> names = new ArrayList<int>();    
required: reference  
found:    int

This is not a completely new answer, more of a sum-up of what’s already said.

With the Java Collections Framework, as the type parameter (in the angle brackets) you name what type of objects you want to store in the Collection (List, Set, …).

By the language design, you cannot use primitive types like int, long, double etc. there, only reference types. So, if you want to store integers in an ArrayList, you have to do ArrayList<Integer> instead of ArrayList<int>.

But later in your code, you want to add Strings to your list. To allow that, your list should be declared ArrayList<String>. An ArrayList<Integer> will give a compile error when trying to add anything that is not an Integer.

You can of course declare an ArrayList<Object>. That one will accept all types of objects (String, Integer, Person, File, …), but that’s against the idea behind generics: to tell the compiler as specifically as possible what type of objects you want to allow in your list (and disallow the others), avoiding strange runtime errors.

And it’s good style to declare something like

List<String> names = new ArrayList<>();


Collection<String> names = new ArrayList<>();

If later you decide that a LinkedList or a HashSet is the data structure that better suits your program’s need, then you just change the right-hand side of the declaration.

Generics don’t work with primitives

List<Integer> names = new ArrayList<>();

You need to use the class Integer instead of primitive type int :

Since jdk1.7 you can use diamond operator <> to avoid code duplicate :

ArrayList<String> names = new ArrayList<>(); 

collection supports object only , use Integer instead of int

as best practice we should not write like this ArrayList<Integer> names = new ArrayList<Integer>();

means always give reference of super so it should be like this

List<Integer> names = new ArrayList<Integer>();

You should use a wrapper class for Integers, not primitive types:

ArrayList<Integer> names = new ArrayList<Integer>();

For more details check here:
Why don't Java Generics support primitive types?