Why are we using Integer here instead of int??
"Integer" in array list?
@bhavik911,
ArrayList
can only reference types, not primitives. Integer
is a class, not a primitive.
When you declare ArrayList<Integer> list1 = new ArrayList<Integer>()
, you’re creating an ArrayList
which will store the Integer
type, not the int
primitive.
but finally when I am adding elements to list by , list.add(2) , am i not adding int elements only here??
A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.
Hence you are declaring the arraylist of the class type Integer