Member-only story

In Java, mind the difference between primitives and their wrappers

Alonso Del Arte
8 min readMar 4, 2020

--

Photo by freestocks on Unsplash

Knowing and understanding the difference between objects and primitives in Java is one of those things that we should expect of every Java programmer, even though in practice it won’t make any difference in program performance.

In some contexts, you can’t use a primitive without causing an error, you must use the wrapper. But in other contexts, you can use either a primitive or a wrapper and it doesn’t seem to make a difference: your program runs, it passes all the tests and delivers the right results.

However, at code review, if anyone asks why you used a wrapper instead of a primitive on such and such line, hopefully you have a better answer than that your finger just happened to press the Shift key.

One of the benefits of using an integrated development environment (IDE) like NetBeans or IntelliJ is that it will alert you to some of the cases where it’s preferable to use a primitive but it’s not an error to use a wrapper.

And of course it’ll also alert you when using a primitive is an error that prevents program compilation and execution.

I expect that everyone reading this knows that Java has eight primitive data types: the integral types byte, short, int and long; the floating point types float and double, the kind of…

--

--

Alonso Del Arte
Alonso Del Arte

Written by Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified

No responses yet