Notable differences between Gosu and Java
The following tables briefly summarize notable differences between Gosu and Java, with particular attention to requirements for converting existing Java code to Gosu. If the Gosu column contains multiple code examples, these examples show the multiple ways in which Gosu can represent the Java code in the Java column. If the Required Change? column contains “Required”, you must make this change to convert existing Java code to Gosu. If this column contains “Optional”, that item is either an optional feature, a new feature, or Gosu optionally permits the Java syntax for this feature.
The following table shows general differences between Gosu and Java.
General difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
Omit semicolons in most code. Gosu supports the semicolon, but standard coding style is to omit it. One exception is in block declarations with multiple statements. |
|
|
Optional. |
Print to console by using the |
|
Gosu style:
Java compatibility style:
|
Optional. |
For Boolean operators, optionally use more natural English versions. Gosu also supports the symbolic versions that Java uses. |
|
Gosu style:
Java compatibility style:
|
Optional. |
Null-safe property accessor paths. |
|
|
Optional. Do not make this change for cases in which you rely on throwing null pointer exceptions. |
The following table shows the differences in functions and variables between Gosu and Java.
Function or variable difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
In function declarations:
|
|
|
Required |
In variable declarations, use the |
|
Type inference: Explicit: Type inference with coercion: |
Required |
In declarations of variable argument functions, also called
Note: Gosu array
initialization syntax is useful for calling these types of methods.
This example demonstrates how to use a collection in a method declaration and
call it using standard Gosu list initializer
syntax: |
|
Method call using array initializer syntax:
Function declaration:
Method call with no arguments:
|
Required |
|
Gosu supports the unary operator assignment statements
Gosu supports other compound assignment statements, such as |
|
|
Required |
|
In an inner class, referring to methods, fields, and properties in the outer
class uses the keyword |
|
Gosu style:
Java compatibility
style:
If there is no ambiguity with an identifier in the inner class, you can omit the
outer. syntax. |
Optional |
| Calling a default method on an interface uses map-access operator syntax. |
|
|
Required |
The following table shows the differences in the type system between Gosu and Java.
Type system difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
For coercions, use
the Optionally, for compatibility, Gosu supports Java-style coercion syntax. |
|
Gosu style:
Java compatibility style:
|
Optional. |
|
Gosu downcasts to a more specific type in |
|
|
Optional. |
Check if an object
is a specific type or its subtypes by using |
|
|
Required. |
To reference the type directly, use |
|
|
Optional. |
|
In Gosu, type names are first-class symbols for the type. Do not get the
|
|
|
Required. |
Types defined natively in Gosu as generic types preserve their type information, including parameterization, at run time. This feature is called reified generics. In contrast, Java removes this information. This feature is called type erasure. From Gosu, Java types lack parameterization even if instantiated in Gosu. For native Gosu types, Gosu preserves type parameterization at run time. |
This code prints: class java.util.ArrayList |
This code prints: java.util.ArrayList<java.lang.Object> Note that For native Gosu types as the main type, Gosu preserves the parameterization as runtime type
information. In the following example, assume
This code prints: my.package.MyClass<java.lang.String> |
Optional for typical use consuming existing Java types. If your code checks type information of native Gosu types, remember that Gosu has reified generics. |
Gosu generics support array-style variance of different generic types. In Java, this usage is a compilation error, even though it is natural to assume that it works. |
In Java, this code causes a compilation error:
|
The analogous Gosu code works:
|
Optional. |
The following table shows the differences in defining classes between Gosu and Java.
Defining classes difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
Declare that you use
specific types or package hierarchies with the keyword |
|
|
Required |
Declare that you use specific static type members with the keyword
uses rather than import. Use the # symbol before
the member name or * symbol, not the period (.) symbol. |
|
|
Required |
To declare one or
more class constructors, write them as functions named |
|
|
Required |
The following table shows the differences in control flow between Gosu and Java.
Control flow difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
The |
|
|
Required |
The Gosu has native support for intervals, which are sequences of values of the same type between a given pair of
end-point values. For example, the set of integers beginning with 0 and ending with 10 has the
shorthand syntax Gosu does not support the |
|
Using Gosu intervals:
Verbose style:
|
Required |
The following table shows optional enhancements that Java does not support and that Gosu provides.
Difference |
Java |
Gosu |
Required change? |
|---|---|---|---|
Gosu enhancements, which support adding methods and properties to any type, including Java types. |
Not applicable |
|
Optional |
Gosu blocks, which are in-line functions that act like objects. These blocks are especially useful with the Gosu collections enhancements. Blocks can also be useful as a shortcut for implementing one-method interfaces. |
Not applicable |
|
Optional |
Native XML support and XSD support. |
Not applicable |
|
Optional |
Native support for consuming web services with syntax similar to native method calls. |
Not applicable |
|
Optional |
Native |
Not applicable |
|
Optional |
Gosu uses the Java-based collections APIs but improves upon them:
For new code, use the Gosu style initialization and APIs. You can call the more verbose Java style for compatibility. |
|
Simplified initialization:
Array-like "set" and "get":
New APIs on Java collections types:
|
Optional |
List and array expansion operator. |
Not applicable |
|
Optional |
See also
