Java Autoboxing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Java =TODO= * https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html")
 
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Java#Subjects|Java]]
* [[Java#Subjects|Java]]
=Overview=
Java has a mechanism called ''boxing'' that converts primitive types (such as <code>int</code>) into their corresponding reference wrapper types (<code>java.lang.Integer</code>). 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.
=Wrapper Classes=
==java.lang.Byte==
Java has a wrapper class named <code>java.lang.Byte</code> that provide a reference type representation to the primitive data type [[Java_Language#byte|byte]].
==java.lang.Integer==
Java has a wrapper class named <code>java.lang.Integer</code> that provide a reference type representation to the primitive data type [[Java_Language#int|int]].
==java.lang.Long==
Java has a wrapper class named <code>java.lang.Long</code> that provide a reference type representation to the primitive data type [[Java_Language#long|long]].
=Autoboxing and Streams API=
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 large stream that uses boxed values uses on average 550% more time 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

Latest revision as of 19:43, 6 April 2020

Internal

Overview

Java has a mechanism called boxing that converts primitive types (such as int) into their corresponding reference wrapper 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.

Wrapper Classes

java.lang.Byte

Java has a wrapper class named java.lang.Byte that provide a reference type representation to the primitive data type byte.

java.lang.Integer

Java has a wrapper class named java.lang.Integer that provide a reference type representation to the primitive data type int.

java.lang.Long

Java has a wrapper class named java.lang.Long that provide a reference type representation to the primitive data type long.

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 large stream that uses boxed values uses on average 550% more time than the same operation on the corresponding specialized stream.

TODO