Users › 03 Types and values

Types and values

Learn the complete type system: seven primitives, strings, arrays, references and null — and exactly what happens when a value does not fit.

1. Primitive types

TypeSizeNotes
bool1 bytetrue / false
byte8-bit signed
short16-bit signed
int32-bit signeddefault integer type
long64-bit signed42L
float32-bit IEEE3.14f
double64-bit IEEE3.14

There are no unsigned integers. Primitives cannot be null. Mixed arithmetic promotes to the common type (byte/short → int → long → float → double), and an arithmetic result is never narrower than int.

2. Conversions are defined, not accidental

Assignment narrows implicitly, like C, but every conversion has a defined result on every platform:

byte b = 300        // 44   (modulo 256, two's complement)
int i = 3.9         // 3    (truncation toward zero)
int j = (int)5e9L   // 705032704 (64→32 truncation)
long k = (long)3.9  // 3

3. Arrays

int[] a = new int[5]
int[][] g = new int[3][3]
int[] lit = {1, 2, 3}
int[][] nested = {{1, 2}, {3, 4}}

4. Strings

String s = "hello"
String t = "n=" + 42 + " " + 2.5 + " " + true + " " + null
int n = s.length()
int at = s.indexOf("llo")
String sub = s.substring(1, 3)

Strings are immutable and cannot be constructed with new. They come from literals, concatenation or substring. Concatenation converts anything on the right-hand side, including null (→ "null"). There is no interpolation. Escapes are \n, \t, \r, \\, \".

5. References and null

There is no Object root type. A reference can be assigned only to its own class, to an interface it implements, or to null. Method parameters and return values accept null; dereferencing it is reported cleanly rather than crashing.

String s = null
s == null            // true — reference comparison, no ___equals
"x" + null           // "xnull"

6. Literals

42         int
42L        long
3.14       double
3.14f      float
0x2A       hexadecimal
0b1010     binary
true       bool
null       null
"hi"       String

Out-of-range numeric literals are compile errors, never silent truncation. Malformed literals (0x, 1e+) are reported with a clear message. .5 is a valid double.

7. Try it

%simple com.example.types.Demo implements Main
    #import Std
    int run(int argc, String[] argv) Main@run {
        byte b = 300
        int i = 3.9
        int[] a = new int[3]
        a[0] = 10
        a[1] = 20
        a[2] = 30
        int[][] g = new int[2][2]
        g[0][1] = 7
        String s = "hello" + " " + 42 + " " + true + " " + null
        Std.print("b=", b, " i=", i, " last=", a[-1], " g01=", g[0][1],
                  " len=", s.length(), " s=", s, "\n")
        return 0
    }
b=44 i=3 last=30 g01=7 len=18 s=hello 42 true null