Using other Java features in Gosu
You can use many other Java features in Gosu.
Implementing Java interfaces
Gosu classes can directly implement Java interfaces. For example:
class MyGosuClass implements Runnable {
function run() {
// your code here
}
}
Gosu also supports its own native interface definitions.
See also
Using Java enumerations
Gosu can directly use Java enumerations.
Gosu also supports its own native enumeration definitions.
See also
Using Java annotations
Gosu can directly use Java annotations.
Gosu also supports its own native annotation definitions.
See also
Using Java primitives
Gosu supports the following primitive types: int, char,
byte, short, long, float,
double, boolean, and the special value null that means an
empty object value. These primitives are the full set that Java supports. The Gosu versions of these primitives
are fully compatible with the Java primitives, in both directions.
null, has an equivalent object type defined in Java. Java does the same for
its primitive types. For example, for int there is the
java.lang.Integer type that descends from the Object
class.- The object types that represent the equivalent of primitive types are called boxed primitive types.
- Primitive types are also called unboxed primitives.
The boxed primitive Java classes can be freely used in Gosu code because of Gosu’s special relationship to the Java language.
In most cases, Gosu converts between boxed and unboxed primitives as needed. However, just as in Java, the differences between the boxed and unboxed types are important.
int and
boolean work differently from objects, which are descendents of the root
Object class. For example:- You can add objects to a collection, but you cannot add primitives.
- Variables of an object type can have the value
null, but variables of a primitive type cannot.
Gosu provides autoboxing, which is to convert values from unboxed to Java-based boxed types as required by a method parameter or return value. Similarly, Gosu provides unboxing, which is to convert values from boxed to unboxed types.
In most cases, Gosu handles differences between boxed and unboxed types for you by automatically converting
values as required. For example, Gosu implicitly converts between the native language primitive type called
boolean and the Java-based object class Boolean
(java.lang.Boolean). If you want explicit coercion, use the as
...NEWTYPE syntax, such as myIntValue as Integer.
If your code implicitly converts a variable’s value from a boxed type to a unboxed type and the value is
null, Gosu standard value type conversion rules apply. For example:
var bBoxed : Boolean
var bUnboxed : boolean
bBoxed = null // bBoxed can genuinely be NULL
bUnboxed = bBoxed // bUnboxed cannot be null, so is converted to FALSE
