Java Autoboxing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 14: Line 14:


The [[Java_8_Streams_API#Autoboxing_and_Specialized_Interfaces|Streams API]] has specialized stream types and API calls for primitive types, to avoid unnecessary autoboxing.
The [[Java_8_Streams_API#Autoboxing_and_Specialized_Interfaces|Streams API]] has specialized stream types and API calls for primitive types, to avoid unnecessary autoboxing.
=Performance Considerations=
{{Warn|The performance penalty associated with autoboxing is quite significant. An operation on a long stream that uses boxed values is on average % slower than the same operation on the corresponding specialized stream.}}


=TODO=
=TODO=


* https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
* https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Revision as of 22:01, 6 April 2018

Internal

Overview

Java has a mechanism called boxing that converts primitive types (such as int) into their corresponding reference types (java.lang.Integer). The opposite operation, converting reference types into their corresponding primitive types is called unboxing.

Autoboxing is a mechanism that performs boxing and unboxing automatically.

Autoboxing comes with a performance cost. Boxed values are wrappers around primitive types, and are stored on the heap. Therefore, boxed values use more memory and requires additional memory lookups to fetch the wrapped primitive type.

Autoboxing and Streams API

The Streams API has specialized stream types and API calls for primitive types, to avoid unnecessary autoboxing.

Performance Considerations


The performance penalty associated with autoboxing is quite significant. An operation on a long stream that uses boxed values is on average % slower than the same operation on the corresponding specialized stream.

TODO