Integer types
- Dart course
- Beginner
- Lesson 13/116
- 4 min
Learn int and num types.
int and num types
In Dart integers are stored in the int type. An int holds a 64-bit integer; the range is about ±9 billion billion. Plenty for everyday calculations.
To declare an int write int age = 25;. You can print it with print(age);.
num is the common supertype of int and double. So a num variable can hold both integers and decimals. But in most cases you use int or double explicitly.
Practice int variable
Declare an integer variable with int age = 25; and print it with print(age);.
Starter code
// Declare an int variableSample solution
void main() {
int age = 25;
print(age);
}Expected output
25
num supertype
num is the common parent of int and double. A num variable can hold an int first, then a double.
For example num x = 5; works; then x = 5.5; is also valid. Both are subtypes of num.
In practice num is rarely used; writing int or double explicitly is clearer. num appears mostly in math function signatures.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Integer type
____ age = 25;
int age = 25;
Explanation: int is used for integers.
2 How many bits is int in Dart?
- 16 bit
- 32 bit
- 8 bit
- 64 bit Correct answer
Explanation: This allows large numbers.
3 Match the types
| Term | Meaning |
|---|---|
int |
Integer |
num |
Number supertype |
double |
Floating-point |
var |
Type inference |