NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Casting
editCasting
editA cast converts the value of an original type to the equivalent value of a
target type. An implicit cast infers the target type and automatically occurs
during certain operations. An explicit cast specifies
the target type and forcefully occurs as its own operation. Use the cast
operator '()'
to specify an explicit cast.
Refer to the cast table for a quick reference on all allowed casts.
Errors
- If during a cast there exists no equivalent value for the target type.
- If an implicit cast is given, but an explicit cast is required.
Grammar
cast: '(' TYPE ')' expression
Examples
-
Valid casts.
declare
int i
; explicit castlong 5
toint 5
→int 5
; storeint 5
toi
declare
Map m
; allocateHashMap
instance →HashMap reference
; implicit castHashMap reference
toMap reference
→Map reference
; storeMap reference
tom
declare
HashMap hm
; load fromm
→Map reference
; explicit castMap reference
toHashMap reference
→HashMap reference
; storeHashMap reference
tohm
Numeric Type Casting
editA numeric type cast converts the value of an original numeric type to the equivalent value of a target numeric type. A cast between two numeric type values results in data loss when the value of the original numeric type is larger than the target numeric type can accommodate. A cast between an integer type value and a floating point type value can result in precision loss.
The allowed casts for values of each numeric type are shown as a row in the following table:
byte |
short |
char |
int |
long |
float |
double |
|
byte |
implicit |
implicit |
implicit |
implicit |
implicit |
implicit |
|
short |
explicit |
explicit |
implicit |
implicit |
implicit |
implicit |
|
char |
explicit |
explicit |
implicit |
implicit |
implicit |
implicit |
|
int |
explicit |
explicit |
explicit |
implicit |
implicit |
implicit |
|
long |
explicit |
explicit |
explicit |
explicit |
implicit |
implicit |
|
float |
explicit |
explicit |
explicit |
explicit |
explicit |
implicit |
|
double |
explicit |
explicit |
explicit |
explicit |
explicit |
explicit |
Examples
-
Valid numeric type casts.
declare
int a
; storeint 1
toa
declare
long b
; load froma
→int 1
; implicit castint 1
tolong 1
→long 1
; storelong 1
tob
declare
short c
; load fromb
→long 1
; explicit castlong 1
toshort 1
→short 1
; storeshort 1
value toc
declare
double e
; load froma
→int 1
; explicit castint 1
todouble 1.0
; storedouble 1.0
toe
; (note the explicit cast is extraneous since an implicit cast is valid) -
Invalid numeric type casts resulting in errors.
Reference Type Casting
editA reference type cast converts the value of an original reference type to the equivalent value of a target reference type. An implicit cast between two reference type values is allowed when the original reference type is a descendant of the target type. An explicit cast between two reference type values is allowed when the original type is a descendant of the target type or the target type is a descendant of the original type.
Examples
-
Valid reference type casts.
declare
List x
; store default valuenull
tox
declare
ArrayList y
; allocateArrayList
instance →ArrayList reference
; storeArrayList reference
toy
;load from
y
→ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
tox
; (noteArrayList
is a descendant ofList
)load from
x
→List reference
; explicit castList reference
toArrayList reference
→ArrayList reference
; storeArrayList reference
toy
;load from
y
→ArrayList reference
; explicit castArrayList reference
toList reference
→List reference
; storeList reference
tox
; (note the explicit cast is extraneous, and an implicit cast is valid) -
Invalid reference type casts resulting in errors.
declare
List x
; allocateArrayList
instance →ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
tox
declare
ArrayList y
; load fromx
→List reference
; error → cannot implicit castList reference
toArrayList reference
; (note an explicit cast is valid sinceArrayList
is a descendant ofList
)declare
ArrayList y
; load fromx
→List reference
; error → cannot explicit castList reference
toMap reference
; (note no cast is valid since neitherList
norMap
is a descendant of the other)
Dynamic Type Casting
editA dynamic (def
) type cast converts the value of an original
def
type to the equivalent value of any target type or converts the value of
any original type to the equivalent value of a target def
type.
An implicit cast from any original type value to a def
type value is always
allowed. An explicit cast from any original type value to a def
type value is
always allowed but never necessary.
An implicit or explicit cast from an original def
type value to
any target type value is allowed if and only if the cast is normally allowed
based on the current type value the def
type value represents.
Examples
-
Valid dynamic type casts with any original type to a target
def
type.declare
def d0
; implicit castint 3
todef
; storeint 3
tod0
allocate
ArrayList
instance →ArrayList reference
; implicit castArrayList reference
todef
→def
; storedef
tod0
declare
Object o
; allocateHashMap
instance →HashMap reference
; implicit castHashMap reference
toObject reference
→Object reference
; storeObject reference
too
declare
def d1
; load fromo
→Object reference
; implicit castObject reference
todef
→def
; storedef
tod1
declare
int i
; load fromd1
→def
; implicit castdef
toHashMap reference
→ HashMap reference`; callsize
onHashMap reference
→int 0
; storeint 0
toi
; (notedef
was implicit cast toHashMap reference
sinceHashMap
is the child-most descendant type value that thedef
type value represents) -
Valid dynamic type casts with an original
def
type to any target type.declare
def d
; implicit castdouble 1.0
todef
→def
; storedef
tod
declare
int i
; load fromd
→def
; implicit castdef
todouble 1.0
→double 1.0
; explicit castdouble 1.0
toint 1
→int 1
; storeint 1
toi
; (note the explicit cast is necessary since adouble
type value is not converted to anint
type value implicitly)store
int 1
tod
; (note the switch in the typed
represents fromdouble
toint
)declare
float i
; load fromd
→def
; implicit castdef
toint 1
→int 1
; implicit castint 1
tofloat 1.0
→float 1.0
; storefloat 1.0
tof
allocate
ArrayList
instance →ArrayList reference
; storeArrayList reference
tod
; (note the switch in the typed
represents fromint
toArrayList
)declare
List l
; load fromd
→def
; implicit castdef
toArrayList reference
→ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
tol
-
Invalid dynamic type casts resulting in errors.
declare
def d
; implicit castint 1
todef
→def
; storedef
tod
declare
short s
; load fromd
→def
; implicit castdef
toint 1
→int 1
; error → cannot implicit castint 1
toshort 1
; (note an explicit cast is valid)allocate
HashMap
instance →HashMap reference
; implicit castHashMap reference
todef
→def
; storedef
tod
declare
List l
; load fromd
→def
; implicit castdef
toHashMap reference
; error → cannot implicit castHashMap reference
toList reference
; (note no cast is valid since neitherHashMap
norList
is a descendant of the other)
String to Character Casting
editUse the cast operator to convert a String
type value into a
char
type value.
Errors
-
If the
String
type value isn’t one character in length. -
If the
String
type value isnull
.
Examples
-
Casting string literals into
char
type values. -
Casting a
String
reference into achar
type value.
Boxing and Unboxing
editBoxing is a special type of cast used to convert a primitive type to its corresponding reference type. Unboxing is the reverse used to convert a reference type to its corresponding primitive type.
Implicit boxing/unboxing occurs during the following operations:
-
Conversions between a
def
type and a primitive type are implicitly boxed/unboxed as necessary, though this is referred to as an implicit cast throughout the documentation. - Method/function call arguments are implicitly boxed/unboxed as necessary.
- A primitive type value is implicitly boxed when a reference type method is called on it.
Explicit boxing/unboxing is not allowed. Use the reference type API to explicitly convert a primitive type value to its respective reference type value and vice versa.
Errors
- If an explicit cast is made to box/unbox a primitive type.
Examples
-
Uses of implicit boxing/unboxing.
declare
List l
; allocateArrayList
instance →ArrayList reference
; storeArrayList reference
tol
;load from
l
→List reference
; implicit castint 1
todef
→def
; calladd
onList reference
with arguments (def
); (note internallyint 1
is boxed toInteger 1
to store as adef
type value)declare
Integer I
; callvalueOf
onInteger
with arguments of (int 0
) →Integer 0
; storeInteger 0
toI
;declare
int i
; load fromI
→Integer 0
; unboxInteger 0
→int 0
; load froml
→List reference
; callget
onList reference
with arguments (int 0
) →def
; implicit castdef
toint 1
→int 1
; storeint 1
toi
; (note internallyint 1
is unboxed fromInteger 1
when loaded from adef
type value) -
Uses of invalid boxing/unboxing resulting in errors.
Integer x = 1; // error Integer y = (Integer)1; // error int a = Integer.valueOf(1); // error int b = (int)Integer.valueOf(1); // error
declare
Integer x
; error → cannot implicit boxint 1
toInteger 1
during assignmentdeclare
Integer y
; error → cannot explicit boxint 1
toInteger 1
during assignmentdeclare
int a
; callvalueOf
onInteger
with arguments of (int 1
) →Integer 1
; error → cannot implicit unboxInteger 1
toint 1
during assignmentdeclare
int a
; callvalueOf
onInteger
with arguments of (int 1
) →Integer 1
; error → cannot explicit unboxInteger 1
toint 1
during assignment
Promotion
editPromotion is when a single value is implicitly cast to a certain type or
multiple values are implicitly cast to the same type as required for evaluation
by certain operations. Each operation that requires promotion has a promotion
table that shows all required implicit casts based on the type(s) of value(s). A
value promoted to a def
type at compile-time is promoted again at run-time
based on the type the def
value represents.
Errors
- If a specific operation cannot find an allowed promotion type for the type(s) of value(s) given.
Examples
-
Uses of promotion.
declare
double d
; promoteint 2
anddouble 2.0 @0
: resultdouble
; implicit castint 2
todouble 2.0 @1
→double 2.0 @1
; adddouble 2.0 @1
anddouble 2.0 @0
→double 4.0
; storedouble 4.0
tod
declare
def x
; implicit castint 1
todef
→def
; storedef
tox
;declare
float f
; load fromx
→def
; implicit castdef
toint 1
→int 1
; promoteint 1
andfloat 2.0
: resultfloat
; implicit castint 1
tofloat 1.0
→float `1.0
; addfloat 1.0
andfloat 2.0
→float 3.0
; storefloat 3.0
tof
; (note this example illustrates promotion done at run-time as promotion done at compile-time would have resolved to adef
type value)
Allowed Casts
editThe following tables show all allowed casts. Read the tables row by row, where the original type is shown in the first column, and each subsequent column indicates whether a cast to the specified target type is implicit (I), explicit (E), or is not allowed (-).
Primitive/Reference Types
o |
b |
s |
c |
i |
j |
f |
d |
O |
B |
S |
C |
I |
L |
F |
D |
T |
R |
def |
|
boolean ( o ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
byte ( b ) |
- |
I |
I |
I |
I |
I |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
short ( s ) |
- |
E |
E |
I |
I |
I |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
char ( c ) |
- |
E |
E |
I |
I |
I |
I |
- |
- |
- |
- |
- |
- |
- |
- |
E |
- |
I |
|
int ( i ) |
- |
E |
E |
E |
I |
I |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
long ( j ) |
- |
E |
E |
E |
E |
I |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
float ( f ) |
- |
E |
E |
E |
E |
E |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
double ( d ) |
- |
E |
E |
E |
E |
E |
E |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Boolean ( O ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Byte ( B ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Short ( S ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Character ( C ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Integer ( I ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Long ( L ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Float ( F ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Double ( D ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
String ( T ) |
- |
- |
- |
E |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
|
Reference ( R ) |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
@ |
I |
@ See reference type casting for allowed reference type casts.
def
Type
o |
b |
s |
c |
i |
j |
f |
d |
O |
B |
S |
C |
I |
L |
F |
D |
T |
R |
def |
|
def as boolean |
I |
- |
- |
- |
- |
- |
- |
- |
I |
- |
- |
- |
- |
- |
- |
- |
- |
- |
|
def as byte |
- |
I |
I |
I |
I |
I |
I |
I |
- |
I |
I |
I |
I |
I |
I |
I |
- |
- |
|
def as short |
- |
E |
I |
E |
I |
I |
I |
I |
- |
E |
I |
E |
I |
I |
I |
I |
- |
- |
|
def as char |
- |
E |
E |
I |
I |
I |
I |
I |
- |
E |
E |
I |
I |
I |
I |
I |
E |
- |
|
def as int |
- |
E |
E |
E |
I |
I |
I |
I |
- |
E |
E |
E |
I |
I |
I |
I |
- |
- |
|
def as long |
- |
E |
E |
E |
E |
I |
I |
I |
- |
E |
E |
E |
E |
I |
I |
I |
- |
- |
|
def as float |
- |
E |
E |
E |
E |
E |
I |
I |
- |
E |
E |
E |
E |
E |
I |
I |
- |
- |
|
def as double |
- |
E |
E |
E |
E |
E |
E |
I |
- |
E |
E |
E |
E |
E |
E |
I |
- |
- |
|
def as Boolean |
I |
- |
- |
- |
- |
- |
- |
- |
I |
- |
- |
- |
- |
- |
- |
- |
- |
||
def as Byte |
- |
I |
I |
I |
I |
I |
I |
I |
- |
I |
I |
I |
I |
I |
I |
I |
- |
- |
|
def as Short |
- |
E |
I |
E |
I |
I |
I |
I |
- |
E |
I |
E |
I |
I |
I |
I |
- |
- |
|
def as Character |
- |
E |
E |
I |
I |
I |
I |
I |
- |
E |
E |
I |
I |
I |
I |
I |
- |
- |
|
def as Integer |
- |
E |
E |
E |
I |
I |
I |
I |
- |
E |
E |
E |
I |
I |
I |
I |
- |
- |
|
def as Long |
- |
E |
E |
E |
E |
I |
I |
I |
- |
E |
E |
E |
E |
I |
I |
I |
- |
- |
|
def as Float |
- |
E |
E |
E |
E |
E |
I |
I |
- |
E |
E |
E |
E |
E |
I |
I |
- |
- |
|
def as Double |
- |
E |
E |
E |
E |
E |
E |
I |
- |
E |
E |
E |
E |
E |
E |
I |
- |
- |
|
def as String |
- |
- |
- |
E |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
I |
- |
|
def as Reference |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
@ |
@ See reference type casting for allowed reference type casts.