Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Performance Notes: Autoboxing / Unboxing

DZone's Guide to

Java Performance Notes: Autoboxing / Unboxing

How features that have been present since Java 1.5 still impact the performance of your apps, and how to substantially improve them.

· Performance Zone
Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

What would you think if I said "We can run the following code snippet 5 times faster after changing just 1 character"?

long t = System.currentTimeMillis();
Long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println("total:" + sum);
System.out.println("processing time: " + (System.currentTimeMillis() - t) + " ms") ;

Output:

total:2305843005992468481
processing time: 6756 ms

After some pondering, you can consider the following, even faster code snippet:

long t = System.currentTimeMillis();
//Long sum = 0L;
long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println("total:" + sum);
System.out.println("processing time: " + (System.currentTimeMillis() - t) + " ms") ;

Output:

total:2305843005992468481

processing time: 1248 ms

We can explain this difference with the careless usage of the "Autoboxing" feature, which has been in our lives since Java 1.5. 

Before continuing the cause of the difference, let's consider "Autoboxing" and "Unboxing" concepts in Java.

Variables in Java are divided into two categories: primitive and reference.  There are 8 primitive types and 8 reference types (wrapper classes) for each primitive type.


Primitive Types Reference Types(Wrapper Class)
boolean Boolean
byte Byte
char Character
float Float
int Integer
long Long
short Short
double Double


"Autoboxing " and "Unboxing" examples can be seen in the following code snippet. In the code snippet, a "long" value is added to a List of "Long". In order to do this in Java 1.4, we must put our primitive variable into an appropriate reference type (boxing). Since Java 1.5, the compiler has done this operation for us. So we have been writing less code.

List<Long> longList = new ArrayList<>();      
long i = 4;
longList.add( i ); //autoboxing      
long j = longList.get( 0 ); //unboxing

Since Java 1.5, the compiler has been changing the above code snippet to the following  snippet automatically:

List<Long> longList = new ArrayList<>();      
long i = 4;
longList.add(Long.valueOf( i ) );      
long j = longList.get( 0 ).longValue();

Hence, we can say that, our first code snippet has been changed to the following one. So, we can explain the slower processing time with the code, creating 2147483647 unnecessary "Long" instances.

long t = System.currentTimeMillis();
Long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += new Long(i);
}
System.out.println("total:" + sum);
System.out.println("processing time: " + (System.currentTimeMillis() - t) + " ms") ;

As a result, we need to consider "Autoboxing" and "Unboxing" concepts in order to write faster Java code.

Resources

Autoboxing and Unboxing
Autoboxing
Efective Java 2nd Edition, J. Bloch

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:
java ,performance ,autoboxing ,unboxing

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}