Java 10 Feature – List Generic Specialization

Last week I wrote about value types, which are one of the new language features that are being investigated for a future version of Java. Next one is “Generic Specialization”.

Under Generic Specialization , you can have a List<int>. The purpose of generic specialization is to make it possible to use primitive types. Below is the example.

List<int> list = new ArrayList<int>();
list.add(1);
list.add(2);
list.add(3);





At first sight you might think: that’s simple, it’s just an extension of auto-boxing, just make List<int> an alias for List<Integer> , but that would not be very satisfactory.

A List<Integer> is a subtype of List<?>, which is a subtype of the raw type List. Because of that we can write code like this:

List<?> list1 = new ArrayList<Integer>();
List list2 = new ArrayList<Integer>();

 

But the same relations will not be true for a List<int>.Error! An ArrayList<int> is not a List<?> nor a raw List because List<T> always expect T as an object

List<?> list1 = new ArrayList<int>();
List list2 = new ArrayList<int>();

 

ALSO READ :- JAVA 10 FEATURE – VALUE TYPE
ALSO READ :- JAVA 9 FEATURES
ALSO READ :- DIFFERENCE IN INTERFACE OF JAVA8, JAVA9, JAVA10



Leave a Reply

Your email address will not be published. Required fields are marked *