Users › 03 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.
| Type | Size | Notes |
|---|---|---|
bool | 1 byte | true / false |
byte | 8-bit signed | |
short | 16-bit signed | |
int | 32-bit signed | default integer type |
long | 64-bit signed | 42L |
float | 32-bit IEEE | 3.14f |
double | 64-bit IEEE | 3.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.
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
float/double → integer saturates Java-style: out-of-range
becomes MIN/MAX, NaN becomes 0.INT_MIN / -1 is safe and returns INT_MIN.int[] a = new int[5]
int[][] g = new int[3][3]
int[] lit = {1, 2, 3}
int[][] nested = {{1, 2}, {3, 4}}
a.length is a field. For strings it is
s.length(), a method — do not mix them up.a[-1] is the last
element.IndexOutOfBoundsException.m[i] returns a view: zero-copy, sharing storage with
the parent. Writing m[0][1] = 9 writes through to the parent.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, \\,
\".
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"
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.
%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