Primitive and boxed types
In Gosu, primitive types such as int and boolean exist primarily for compatibility
with the Java language. Gosu uses these Java primitive types to support
extending Java classes and implementing Java interfaces.
From a Gosu language perspective, primitives
are different only in subtle ways from object-based types such as Integer and Boolean. Primitive types can be
automatically coerced to non-primitive versions or back again by the
Gosu language in almost all cases. For example, int can be cast to Integer and Boolean cast to boolean. In typical code, you
do not need to know the differences.
The boolean
type is a Java primitive which is also called an “unboxed” type.
In contrast, Boolean is
a class that implements a “boxed” type of the boolean primitive. A boxed type
is a primitive type wrapped in the shell of a class. Boxed types are
useful for code that requires all values to have the common ancestor
type Object. For example,
Gosu collection data types typically require members to have types that
descend from Object. Thus,
collections can contain Integer
and Boolean, but not the
primitives int or boolean.
The Gosu boxed types use the Java primitive
types. The boxed types are defined in the java.lang package, such as java.lang.Integer. Code execution
performance is slightly improved when using a primitive rather than its
boxed version.
There is an important difference between
primitive and boxed types when handling uninitialized values. Variables
declared of a primitive type cannot hold the null value. However, null can be assigned to variables
of type Object and any
subtype of Object.
