Java Autoboxing Performance Impact

This post will introduce you to Java Autoboxing Performance Impact. Java provides the Autoboxing technique that helps developers write code cleaner and easier to read. But it also brings to use some impact of performances. If you are not familiar with the Java Autoboxing technique you can have a look at the tutorial¬† Autoboxing and Unboxing Java Example. Let’s dig deeper.

Java Autoboxing Performance Impact

Use Autoboxing

We consider the java program below

Run the above program, it will print the output below.

Java Autoboxing Performance Impact

You can see in the output, the calculation time is 9 seconds. It is quite slow. The reason comes from the mixed-used of primitive type and wrapper class and the boxed and unboxed activities are repeatedly.

Use primitive type

Now we change a bit the above program, we just use a primitive type for the sum variable as longtype. We have a program like this:

Run the changed program, it will print the output below

As you see, the calculation time is just 1 second. It is faster than the first program.

So far, you have experienced Java Autoboxing Performance Impact in this post and learn from that to apply your code to get the best performance for your java program.

Happy Learning.

References
Autoboxing and Unboxing Java Example


Autoboxing in Java

Please share it if you found this useful
Hide Buttons