03. Autoboxing

One of the restrictions with Java Generics is that they must be instantiated with a reference type. This means you cannot use a primitive type such as int, char or double. Instead, we must use its corresponding non-primitive type (eg. Integer, Character, Double).

Luckily, Java automatically converts a primitive type to its corresponding reference type; this is known as autoboxing..

So for example, when we place an item into our container of primitive type, such as the number 50, converts it an equivalent refernece type.

Thus we have int morphing into Integer.

// is actually doing this...
intContainer.add(new Integer(50));

In contrast, auto-unboxing occurs when retrieving that same item from our collection.

// from Integer to int
int i = intContainer.getData(); 

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Learn Enterprise Java Development for a Bright Career

Core Java, Volume II

Learn Enterprise Java Development for a Bright Career Try Java

Designed for serious programmers, this reliable, unbiased, no-nonsense tutorial illuminates advanced Java language and library features with thoroughly tested code examples. As in previous editions, all code is easy to understand and displays modern best-practice solutions to the realworld challenges faced by professional developers.

$ Check price
59.9959.99Amazon 4 logo(34+ reviews)

More Java resources