Boxing and unboxing in Scala
Abstract
Scala offers automatic boxing and unboxing. This newsletter describes the different variations and gives advice for its use.
I'm living in Stuttgart now for a few days now and I really enjoy it. I warmly recommend this place to everyone who would like to see one of the prettier places of Germany. This location is especially suited to work on an issue of the Scala newsletter. I would love to get some feedback. You can use the contact form for this purpose.
Boxing
Boxing by means of implicit
In the module Scala.Predef are definitions of methods for the implicit type conversion. For example the method Predef.int2Integer defines the automatic converting (i.e. boxing) of Int to java.lang.Integer.
This boxing is applied whenever an instance of the type scala.Int is used but the java.lang.Integer is expected. Boxing by means of implicit is nothing more than the application of the available language features.
The implicit converting is defined for the types Boolean, Byte, Short, Char, Int, Long, Float and Double.
![]() | Note |
|---|---|
A call to this converting method always creates a new instance of the respective reference datatype. Contrary to Java there's no caching in Scala for the classes Byte, Short, Char, Int and Long. |
Compiler generated boxing
If an operation is executed which expects an instance of java.lang.Object instead of a primitive data type the compiler inserts an automatic type conversion. This is the case if one of the methods of java.lang.Object is called (toString(), hashCode(), ...), if a value-type is printed using println or if a value is appended to a string.
This generated conversion uses methods of the class scala.runtime.BoxesRunTime instead of the implicit method definitions. The values of the type Boolean are cached. Only certain values are cached for the classes Character, Byte, Short, Integer and Long. Java's cache mechanism is not used, though.
![]() | Note |
|---|---|
Martin Odersky explained on the Scala mailing list, that it is planned to use Java's methods for boxing in both variants of Scala's boxing. |
def printCount(count: Int) = println("Count: " + count)
Explanation. For this statement two additional objects are generated: One instance of the class scala.StringBuilder for the creation of the string and one instance of java.lang.Integer which represents the value of count.
The integer value is appended by a call to the method StringBuilder.append(Object). The compiler could generate better code by using the variant StringBuilder.append(Int). Currently an unnecessary instance of java.lang.Integer is created.
Attention should be paid to the fact that Scala's value types can be used as type parameters. But internally the corresponding referece type of Java is used instead. This means that in spite of a type parameter scala.Int the reference type java.lang.Integer is used instead.
scala.Int as type parameter
def testSortedSet() = {
val list: List[Int] = 1 :: 2 :: 3 :: Nil
val headAsInt: Int = list.head
val headAsInteger: java.lang.Integer = list.head
}
Explanation. This example looks very elegantly but in fact it isn't. Intuitionally you would expect a list of scala.Int. But the compiler generates code which uses Java's reference type java.lang.Integer instead.
For each of the numerals 1, 2, 3 the method BoxesRunTime.boxToInteger is called to return an instance of Integer.
For the assignment val headAsInt: Int = list.head an unboxing to scala.Int is performed.
And for val headAsInteger: java.lang.Integer = list.head it's unnecessarily converted to Int and later on converted back to java.lang.Integer for the assignment.
![]() | Warning |
|---|---|
It's inefficient and confusing to use Scala's value types as type parameters.It's better to use the corresponding Java reference types, e.g. java.lang.Integer. |
Unboxing in Scala
Contrary to the boxing there is no automatic, implicit unboxing in Scala. This means that Java's reference data types are not automatically converted into Scala's value types. To achieve this additional method definitions are necessary.
java.lang.Integer
implicit def unboxInt(i: java.lang.Integer) = i.intValue
Explanation. This method for the implicit type conversion is used whenever a value of the type java.lang.Integer is used instead of the expected type scala.Int.
Analogical definitions are necessary for the types java.lang.Boolean, java.lang.Character, etc.
As already mentioned above an automatic conversion takes place for value types which are used as type parameters. The class seems to use primitive data types but internally it works with Java's reference data types.
Recommendations
- In a pure Scala program Java's reference data types should not be used. Scala value types should be used instead.
- It leads to inefficient code if a primitive data type is used as type parameter. If possible change your source code so it's not needed any more. As a quick but not optimal solution the corresponding reference type of Java can be used.
- Boxing and unboxing can induce an increased usage of memory and an increased running time. If Java libraries are used in a Scala software you should especially look out for problems due to automatic boxing or unboxing.

![[Note]](../images/note.png)
![[Warning]](../images/warning.png)