Following code works perfect and adds 1 and 2 values to the list, but why?
Why you don't need to create Short objects explicitly? e.g: list.add(new Short(1));
List<Short> list = new ArrayList();
list.add((short)1);
list.add((short)2);
System.out.println(list);
This is called autoboxing. It is a feature that automatically converts primitives to their corresponding object type. It is present since Java 1.5.
The opposite of autoboxing is called autounboxing but beware of NullPointerException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With