---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 5. Conversions and Contexts"
section: "5.1.7"
heading: "5.1.7. Boxing Conversion"
parent: "5.1. Kinds of Conversion"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.1.7
id: "5.1.7"
url: https://jvm.obfuscated.software/jls/se17/5.1.7.html
raw: https://jvm.obfuscated.software/jls/se17/5.1.7.md
---

# 5.1.7. Boxing Conversion

Boxing conversion treats expressions of a primitive type as expressions of a corresponding reference type. Specifically, the following nine conversions are called the *boxing conversions*:

- From type `boolean` to type `Boolean`

- From type `byte` to type `Byte`

- From type `short` to type `Short`

- From type `char` to type `Character`

- From type `int` to type `Integer`

- From type `long` to type `Long`

- From type `float` to type `Float`

- From type `double` to type `Double`

- From the null type to the null type

  This rule is necessary because the conditional operator ([§15.25](15.25.md)) applies boxing conversion to the types of its operands, and uses the result in further calculations.

At run time, boxing conversion proceeds as follows:

- If `p` is a value of type `boolean`, then boxing conversion converts `p` into a reference `r` of class and type `Boolean`, such that `r.booleanValue() == p`

- If `p` is a value of type `byte`, then boxing conversion converts `p` into a reference `r` of class and type `Byte`, such that `r.byteValue() == p`

- If `p` is a value of type `char`, then boxing conversion converts `p` into a reference `r` of class and type `Character`, such that `r.charValue() == p`

- If `p` is a value of type `short`, then boxing conversion converts `p` into a reference `r` of class and type `Short`, such that `r.shortValue() == p`

- If `p` is a value of type `int`, then boxing conversion converts `p` into a reference `r` of class and type `Integer`, such that `r.intValue() == p`

- If `p` is a value of type `long`, then boxing conversion converts `p` into a reference `r` of class and type `Long`, such that `r.longValue() == p`

- If `p` is a value of type `float` then:

  - If `p` is not NaN, then boxing conversion converts `p` into a reference `r` of class and type `Float`, such that `r.floatValue()` evaluates to `p`
  - Otherwise, boxing conversion converts `p` into a reference `r` of class and type `Float` such that `r.isNaN()` evaluates to `true`

- If `p` is a value of type `double`, then:

  - If `p` is not NaN, boxing conversion converts `p` into a reference `r` of class and type `Double`, such that `r.doubleValue()` evaluates to `p`
  - Otherwise, boxing conversion converts `p` into a reference `r` of class and type `Double` such that `r.isNaN()` evaluates to `true`

- If `p` is a value of any other type, boxing conversion is equivalent to an identity conversion ([§5.1.1](5.1.1.md)).

If the value `p` being boxed is the result of evaluating a constant expression ([§15.29](15.29.md)) of type `boolean`, `byte`, `char`, `short`, `int`, or `long`, and the result is `true`, `false`, a character in the range `'\u0000'` to `'\u007f'` inclusive, or an integer in the range `-128` to `127` inclusive, then let `a` and `b` be the results of any two boxing conversions of `p`. It is always the case that `a` `==` `b`.

Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part. This allows (but does not require) sharing of some or all of these references.

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all `char` and `short` values, as well as `int` and `long` values in the range of -32K to +32K.

A boxing conversion may result in an `OutOfMemoryError` if a new instance of one of the wrapper classes (`Boolean`, `Byte`, `Character`, `Short`, `Integer`, `Long`, `Float`, or `Double`) needs to be allocated and insufficient storage is available.
