Sub topic 12
Converting and casting between types
This chapter assumes you can declare variables and know the primitive types from earlier chapters. It covers how Apex moves a value from one type to another, and what happens when a conversion is not possible.
Three kinds of conversion
Apex is strongly typed, so a value only changes type in one of three ways:
- Implicit widening, which Apex does for you.
- An explicit cast, written as
(Type) value. - A conversion method such as
valueOf,intValue, ortoString.
Implicit widening
Apex converts automatically when no information can be lost. Numeric types widen in this order:
An Integer can be assigned to a Long, a Long to a Decimal, and so on. The reverse direction is never implicit.
Casting from Object
A cast tells the compiler you know the real type of a value. It is mostly used when a variable is declared as Object or SObject.
Object value = 'text';
String asString = (String) value;
Integer parsed = Integer.valueOf('42');
Integer small = 7;
Long widened = small;
Decimal asDecimal = widened;
System.debug(asString + ' ' + parsed + ' ' + asDecimal);If the real type does not match, the cast fails at runtime with a System.TypeException:
Object value = 'text';
try {
Integer n = (Integer) value;
} catch (TypeException e) {
System.debug('Bad cast: ' + e.getMessage());
}Narrowing with methods
Going from a wider type to a narrower one needs a method, because you may lose digits.
Decimal price = 19.99;
Integer whole = price.intValue();
Long big = price.longValue();
String text = String.valueOf(price);
System.debug(whole + ' ' + big + ' ' + text);intValue() drops the fraction. It does not round. Use setScale first if you need rounding.
Parsing strings
valueOf methods parse text into a typed value. Bad input throws a TypeException, so wrap user-supplied text in try/catch.
String input = 'abc';
Integer result;
try {
result = Integer.valueOf(input);
} catch (TypeException e) {
result = 0;
}
System.debug(result);Date parsing works the same way: Date.valueOf('2026-11-30') expects yyyy-MM-dd.
SObject casts
A generic SObject variable can hold any object. Cast it to a specific type before you read fields that belong to that type.
SObject record = new Account(Name = 'Acme');
Account acc = (Account) record;
System.debug(acc.Name);Casting to the wrong SObject type, such as (Contact) record here, throws a TypeException.
Common errors
| Mistake | Result |
|---|---|
| Assigning a `Decimal` to an `Integer` directly | Compile error. Use `intValue()`. |
| `Integer.valueOf('12.5')` | Runtime `TypeException`. Parse as `Decimal` instead. |
| Casting `Object` to the wrong type | Runtime `TypeException`. |
| Assuming `intValue()` rounds | It truncates. |
Exercise
Why does Integer count = 3.7; fail to compile, and how do you get the value 3 into an Integer variable from a Decimal named d holding 3.7?
Show answer
Decimal is wider than Integer, and Apex only widens implicitly, never narrows. Use Integer count = d.intValue();, which truncates the fraction to give 3.