Java primitive wrappers show you the way
By: Tom Henricksen
Tags: Learning To Code, Programming, Software Development, Java

Java primitive wrappers help us to move between types. As a Java developer, it is important to understand the data types. In this post let’s learn some basics about the Java Primitive Wrappers.

“Weniger aber besser. The English translation is: Less but better.”

― Greg McKeown, Essentialism: The Disciplined Pursuit of Less

In Essentialism: The Disciplined Pursuit of Less Greg McKeown describes how we can follow the pursuit of less. I know you are saying I want more. We all want more stuff, bigger houses, faster cars, etc. Greg relates the story of this quote, “Less but better.” It is from Dieter Rams an industrial designer at Braun. As software engineers, we need to keep this in mind. We can dream up solutions that look more like a Rube Goldberg machine than an essential solution.

Java Primitive Wrappers

To use primitive Java objects they have added wrapper classes to allow us to use them more effectively. Let’s talk about those wrapper classes and define them. Then we can discuss the benefits see them in use and explore what they can do.

Define

Java has primitive variables that are the only thing that is not true objects in the language. If we need to work with the many jobs require us to convert them to objects. This is where the wrapper classes fit in. For instance, if you want to put them into a collection they must be converted to objects. This is good information to know as it can come up on job interviews.

Benefits

Other benefits for wrapper classes are that if you need to pass a primitive to a method that requires an object you can use a wrapper class. These wrapper classes can help convert to other types as well. If you have an int and need to make it a String, this can be done with parseInt() method. Wrapper classes can handle these for you easily. The Java Integer class can convert to double(doubleValue()) or float(floatValue()).

Convert

Java’s Wrapper classes allow us to go from primitive to object and back. Enough talk let’s see it in action.

Here we start with primitive ints and convert them to Integer objects with the wrapper class. Then we convert them back to primitive variables. Note that we can convert it back to a different primitive in the case of the cardsRuns to byte type.

The primitive wrapper classes help us put primitive elements into a collection. If we want to put a bunch of ints into an ArrayList we need to convert them. This is where our friends the primitive wrapper classes can be quite helpful. We can iterate over the collection and work on them.

Autobox

Starting in Java 5 we no longer had to manually change from the primitive to the wrapper class. This is called autoboxing. We can walk through an example.

This example runs through taking a primitive int and converting to an object and back. Using autoboxing is quite easy compared to the older way.

Java gives us the primitive wrapper classes to aid in our work. Like a shoehorn, it helps us fit things into different style containers. They are beneficial when we need to move primitives into a collection or work on them in some fashion. Take some time to play with the conversions yourself and gain some understanding.

How have you used Java primitive wrappers?

Check out more great content and subscribe at MyITCareerCoach.com