0
# Boxing and Unboxing Operations
1
2
Comprehensive boxing/unboxing utilities with arithmetic, bitwise, and logical operators for primitive types. The BoxesRunTime object provides runtime support for Scala's unified type system, handling type coercion and operations on boxed primitive values.
3
4
## Core Imports
5
6
```scala
7
import scala.runtime.{BoxesRunTime, BoxedUnit}
8
```
9
10
## Capabilities
11
12
### Boxing Operations
13
14
Converts primitive values to their corresponding boxed (object) representations.
15
16
```scala { .api }
17
object BoxesRunTime {
18
def boxToBoolean(v: Boolean): java.lang.Boolean
19
def boxToCharacter(v: Char): java.lang.Character
20
def boxToByte(v: Byte): java.lang.Byte
21
def boxToShort(v: Short): java.lang.Short
22
def boxToInteger(v: Int): java.lang.Integer
23
def boxToLong(v: Long): java.lang.Long
24
def boxToFloat(v: Float): java.lang.Float
25
def boxToDouble(v: Double): java.lang.Double
26
}
27
```
28
29
### Unboxing Operations
30
31
Extracts primitive values from their boxed representations, returning default values for null inputs.
32
33
```scala { .api }
34
object BoxesRunTime {
35
def unboxToBoolean(o: java.lang.Object): Boolean
36
def unboxToChar(o: java.lang.Object): Char
37
def unboxToByte(o: java.lang.Object): Byte
38
def unboxToShort(o: java.lang.Object): Short
39
def unboxToInt(o: java.lang.Object): Int
40
def unboxToLong(o: java.lang.Object): Long
41
def unboxToFloat(o: java.lang.Object): Float
42
def unboxToDouble(o: java.lang.Object): Double
43
}
44
```
45
46
### Arithmetic Operations
47
48
Performs arithmetic operations on boxed numeric values with automatic type promotion.
49
50
```scala { .api }
51
object BoxesRunTime {
52
def add(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
53
def subtract(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
54
def multiply(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
55
def divide(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
56
def takeModulo(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
57
def negate(arg: java.lang.Object): java.lang.Object
58
def positive(arg: java.lang.Object): java.lang.Object
59
}
60
```
61
62
### Bitwise Operations
63
64
Performs bitwise operations on integral boxed values.
65
66
```scala { .api }
67
object BoxesRunTime {
68
def takeAnd(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
69
def takeOr(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
70
def takeXor(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
71
def complement(arg: java.lang.Object): java.lang.Object
72
def shiftSignedLeft(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
73
def shiftSignedRight(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
74
def shiftLogicalRight(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
75
}
76
```
77
78
### Logical Operations
79
80
Performs logical operations on boolean boxed values.
81
82
```scala { .api }
83
object BoxesRunTime {
84
def takeConditionalAnd(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
85
def takeConditionalOr(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
86
def takeNot(arg: java.lang.Object): java.lang.Object
87
}
88
```
89
90
### Comparison Operations
91
92
Performs comparison operations on boxed values with proper type handling.
93
94
```scala { .api }
95
object BoxesRunTime {
96
def equals(x: java.lang.Object, y: java.lang.Object): Boolean
97
def testEqual(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
98
def testNotEqual(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
99
def testLessThan(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
100
def testLessOrEqualThan(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
101
def testGreaterThan(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
102
def testGreaterOrEqualThan(arg1: java.lang.Object, arg2: java.lang.Object): java.lang.Object
103
}
104
```
105
106
### Type Checking and Conversion
107
108
Checks types and converts between different numeric representations.
109
110
```scala { .api }
111
object BoxesRunTime {
112
def isBoxedNumber(arg: java.lang.Object): Boolean
113
def isBoxedNumberOrBoolean(arg: java.lang.Object): Boolean
114
def toCharacter(arg: java.lang.Object): java.lang.Character
115
def toByte(arg: java.lang.Object): java.lang.Byte
116
def toShort(arg: java.lang.Object): java.lang.Short
117
def toInteger(arg: java.lang.Object): java.lang.Integer
118
def toLong(arg: java.lang.Object): java.lang.Long
119
def toFloat(arg: java.lang.Object): java.lang.Float
120
def toDouble(arg: java.lang.Object): java.lang.Double
121
}
122
```
123
124
## Usage Examples
125
126
```scala
127
import scala.runtime.BoxesRunTime
128
129
// Basic boxing and unboxing
130
val x = 42
131
val boxed = BoxesRunTime.boxToInteger(x)
132
val unboxed = BoxesRunTime.unboxToInt(boxed)
133
134
// Arithmetic operations
135
val a = BoxesRunTime.boxToInteger(10)
136
val b = BoxesRunTime.boxToInteger(5)
137
val sum = BoxesRunTime.add(a, b)
138
val difference = BoxesRunTime.subtract(a, b)
139
val product = BoxesRunTime.multiply(a, b)
140
141
// Comparison operations
142
val isEqual = BoxesRunTime.equals(a, b)
143
val isLess = BoxesRunTime.testLessThan(a, b)
144
145
// Type conversion
146
val asDouble = BoxesRunTime.toDouble(a)
147
val asFloat = BoxesRunTime.toFloat(a)
148
```
149
150
## Types
151
152
### BoxedUnit Support
153
154
```scala { .api }
155
class BoxedUnit extends java.io.Serializable {
156
override def equals(that: Any): Boolean
157
override def hashCode(): Int
158
override def toString: String
159
}
160
161
object BoxedUnit {
162
def UNIT: BoxedUnit // Note: Implementation uses `???` - not functional
163
def TYPE: Class[Unit]
164
}
165
```