Type Cast Operator

Summary

Cast an expression to a specific type.

Syntax

(type) expression

Parameters

type
The new type to interpret the expression as or convert the expression to.
expression
A valid expression.

Description

The type cast operator will reinterpret an expression using a different type or convert the expression to a new type, depending on the context in which it is used. For primitive types, the values will be converted. For user-defined types, the type will be reinterpreted.

An example where casts can be useful are when you cannot implicitly convert an unsigned int to int:

1
2
unsigned int x = 1;
int y = x;

[ ERROR ] JSPPE5016: Cannot convert `unsigned int' to `int'. A cast is available

However, since we know the value 1 (one) is within the range of an int, if we apply an explicit cast, this operation will be allowed:

1
2
unsigned int x = 1;
int y = (int) x;

Explicit casts should only be used when you are sure the values will fit within the range of the type being casted to. For example, it would not be a good idea to cast a value that you know can be potentially negative to an unsigned int, since unsigned numeric types cannot be negative. Otherwise, unintended consequences can occur when the cast occurs at runtime.

See the Conversions Table to see which types can be explicitly cast to another. When casting primitive data types, runtime conversions will be inserted.

Casting Array and Function Types

When casting to an array or function type, a runtime conversion will be inserted. Functions will simply be converted directly to the new type. For arrays, if the conversion fails, an empty array will be returned.

When converting from external to an array or function, an explicit cast is always necessary.

Casting with User-defined Types

When casting to user-defined types, such as classes, no runtime conversions are inserted; however, a runtime check is performed. Attempting to cast to an invalid type will result in a runtime System.Exceptions.CastException.

Downcasting

The act of casting a base class down to one of its derived classes is known as downcasting.

Consider the following inheritance hierarchy and variable declaration:

1
2
3
4
5
class Animal {}
class Dog : Animal {}
class Cat : Animal {}
 
Animal animal = new Dog();

In the above code, it is acceptable to cast animal to Dog since it will indeed be an instance of Dog at runtime. However, a cast to Cat would result in a runtime System.Exceptions.CastException exception being thrown.

An example where downcasting can be useful is when dealing with arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import System;
 
class Animal {}
class Dog : Animal {
    public void talk() {
        Console.log("woof");
    }
}
 
Animal[] animals = [];
animals.push(new Dog());
 
Dog dog = (Dog) animals[0];
dog.talk();

In the above example, we know the first element of the array animals is an instance of Dog, so we cast to Dog in order to use the element individually.

Upcasting

Explicit upcasting (casting from a derived class to a base class) is not required, but it can be done:

1
2
3
4
class Animal {}
class Dog : Animal {}
 
Animal animal = (Animal) new Dog();

When upcasting (whether implicitly or explicitly), no runtime checks are inserted, as is the case with downcasting, as they are unnecessary; the compiler can prove the types are correct at compile time.

Examples

Casting an 'unsigned int' to 'int'
1
2
unsigned int x = 1;
int y = (int) x;
Basic Downcasting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import System;
 
class Animal {}
class Dog : Animal {
    public void talk() {
        Console.log("woof");
    }
}
class Cat : Animal {
    public void talk() {
        Console.log("meow");
    }
}
 
Animal a = new Dog();
// a.talk(); // This would be an error because 'Animal' has no method 'talk'
((Dog) a).talk(); // "woof"
Casting from 'external' to 'int[]'
1
2
3
external foo;
 
int[] bar = (int[]) foo;
Casting from 'external' to 'int()'
1
2
3
external foo;
 
int() bar = (int()) foo;

See Also

Share

HTML | BBCode | Direct Link