No need for creating Short objects explicitly?

java

Solution

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

Problem

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); ```

Original source