DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Harnessing the Power of SIMD With Java Vector API
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Avoiding If-Else: Advanced Approaches and Alternatives
  • Dust: Open-Source Actors for Java

Trending

  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  1. DZone
  2. Coding
  3. Java
  4. Java Performance Notes: Autoboxing / Unboxing

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.

By 
Ali Kemal TASCI user avatar
Ali Kemal TASCI
·
Apr. 09, 16 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
21.7K Views

Join the DZone community and get the full member experience.

Join For Free

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 TypesReference 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

Java (programming language) Java performance Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Harnessing the Power of SIMD With Java Vector API
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Avoiding If-Else: Advanced Approaches and Alternatives
  • Dust: Open-Source Actors for Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!