0

the "Integer" datatype in java?

import java.util.ArrayList; import java.util.Collections; public class MyClass { public static void main(String[ ] args) { ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(36); nums.add(73); nums.add(40); nums.add(1); Collections.sort(nums); System.out.println(nums); } } here in ArrayList we are defining the type of values stored inside the list are of integer type... but why we are writing ArrayList<Integer> and not ArrayList<int> ?

5th Oct 2017, 8:06 AM
LALIT NEGI
LALIT NEGI - avatar
2 Réponses
+ 4
Integer is a class while int is a primitive data type. These class is also called a Wrapper Class ArralyList doesnt accept or stores primitive data types like int but accepts only objects like Integer. the reason behind is that ArrayList is implemented to use objects only.
5th Oct 2017, 8:29 AM
vincent
vincent - avatar
+ 1
thanks
5th Oct 2017, 8:45 AM
LALIT NEGI
LALIT NEGI - avatar