⚡ Code it · 40 XP
Primitive types, null and type conversion
Primitive types, null and conversion
Apex is statically typed: every variable has a type, and the compiler checks it.
| Type | Example | Notes |
|---|---|---|
Integer | 42 | 32-bit |
Long | 42L | 64-bit |
Decimal | 19.99 | Use for money. Currency fields are Decimals |
Double | 3.14 | Floating point |
String | 'hello' | Single quotes only |
Boolean | true | |
Date, Datetime, Time | Date.today() | |
Id | '001...' | A validated 15/18-char record Id |
Everything can be null
Any variable that isn't assigned is null, including Integers and Booleans. Using null in arithmetic throws a NullPointerException:
Integer bonus; // null
Integer total = 10 + bonus; // 💥 NullPointerException
Guard with a ternary, or with the null-coalescing operator ??:
Integer safe = bonus == null ? 0 : bonus;
Integer alsoSafe = bonus ?? 0;
Converting
Integer i = Integer.valueOf('42'); // String → Integer
String s = String.valueOf(42); // Integer → String
String t = 'XP: ' + 42; // concatenation converts for you
Decimal d = Decimal.valueOf('19.99');
Your mission
The variables xpText and bonus already exist (Quill declared them for you). Follow the four steps in the editor. Variable names matter: Quill checks total, summary and safeBonus.