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> ?
2 Answers
+ 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.
+ 1
thanks