Data Types

Anonymous contributor's avatar
Anonymous contributor
Published Jan 27, 2024Updated May 21, 2024
Contribute to Docs

In Dart, data types are stored in variables, just like in other languages like C, Python or Java, and since Dart is a type-safe language, to guarantee that a variable’s value always matches the variable’s static type, it combines runtime and static type checking. Meaning that trying to store a string in an integer variable would not work.

What is Static Typing?

Static typing refers to the property of the programming language where variables are bound to their types at compile-time. It means that the type of a variable is known and checked by the compiler before the code is executed, allowing for early detection of type-related errors. In the example below all variables (myInteger, myDouble, myBoolean, and myString) are declared with an explicit data type (int, double, bool, and String, respectively):

int myInteger = 42;
double myDouble = 3.14;
bool myBoolean = true;
String myString = "Hello, Codecademy!";

Below are a couple of examples of incorrect declarations of values to variables:

// Non-integer value to an int variable
int myInteger = "42";
// Omitted the explicit data type for a variable
var missingType = 3.14;
// Declared a boolean variable but provided a string value
bool myBoolean = "false";
// Mismatch between declared type and assigned value
String myString = 123;

Numbers

Numbers are used to hold numeric values. They can be classified as int, num, and double:

int wholeNumber = 42;
double floatingPointNumber = 3.14;
num inheritedNumber = 7; // num can hold both int and double values
print(wholeNumber); // Output: 42
print(floatingPointNumber); // Output: 3.14
print(inheritedNumber); // Output: 7

Strings

Strings String represent a sequence of characters and are enclosed in either single quotes ('...') or double quotes ("..."):

String greeting = "Double quotes work!";
String anotherString = 'Single quotes work too!';
print(greeting); // Output: Double quotes work!
print(anotherString); // Output: Single quotes work too!

Booleans

Booleans bool represent values that can be either true or false:

bool isDartFun = true;
bool isPythonFun = false;
print(isDartFun); // Output: true
print(isPythonFun); // Output: false

Symbols

Symbol Symbol object represents an operator or identifier. Objects are particularly valuable for APIs that identify elements by name. This is crucial for APIs because while minification can alter identifier names, it does not impact the underlying identifier symbols:

Symbol goodSymbol = Symbol('myIdentifier');
print(goodSymbol); // Output: Symbol("myIdentifier")

Lists

List List represents a collection of an ordered group of objects, working similar to arrays in other programming languages:

List<int> numbersList = [1, 2, 3, 4, 5];
print(numbersList); // Output: [1, 2, 3, 4, 5]

Maps

Map Map object consists of a key-value pair and it can be any data type:

Map<String, int> studentScores = {
'Alice': 95,
'Bob': 87,
'Charlie': 92,
};
print(studentScores['Alice']); // Output: 95
print(studentScores['Bob']); // Output: 87

Nulls

Nulls Null is a type that represents the absence of a value in a variable. This type can only have one possible value, which is null. Dart uses a sound null safety system, meaning that variables are non-nullable by default, and they have to be explicitly declared as nullable if they need to be able to hold a null value:

int? nullableInt; // Nullable integer
String? nullableString; // Nullable string
print(nullableInt); // Output: null
print(nullableString); // Output: null

Runes

Runes Runes exposes the Unicode code points of a string. Unicode is a character encoding standard that aims to encompass all characters used by humans for writing:

String greeting = 'Hello 👋'; // String with emoji
// Using Runes to iterate over the Unicode code points
for (var codePoint in greeting.runes) {
print('Code Point: $codePoint, Character: ${String.fromCharCode(codePoint)}');
}

What is Dynamic?

Dynamic typing is checked dynamically at runtime rather than statically at compile-time. It introduces flexibility but at the same time sacrifices some of the benefits of static typing:

dynamic flexibleVariable = 42;
print(flexibleVariable); // Output: 42

Below are some more examples:

int staticVariable = 42; // Non-dynamic, statically checked variable
dynamic dynamicVariable = "Hello";
int resultDynamic = dynamicVariable + 10; // Error: Dynamic type can lead to runtime errors
int resultStatic = staticVariable + 10; // Correct: Static type checking
print(resultDynamic); // Outcome: Error
print(resultStatic); // Output: 52

Comparison of Data Types in Different Languages

The following table compares the data types in Dart with the data types in other languages:

Data Type Dart Kotlin Java Swift
Integers int Int, Byte, Short, Long int, byte, short, long Int, Int8, Int16, Int32, Int64
Floating-Point double Double, Float double, float Double, Float
Boolean bool Boolean boolean Bool
String String String String String
Arrays List<T> (generic) Array<T> (generic) T[] (specific types) Array<T> (generic)
Maps Map<K, V> (generic) Map<K, V> (generic) HashMap<Key, Value> Dictionary<Key, Value> (generic)
Null Safety Non-nullable by default, ? for nullable Nullable by default, !! for non-null Primitive types not nullable, objects can be null Non-nullable by default, ? for optional, ! for forced unwrapping

All contributors

Contribute to Docs

Learn Dart on Codecademy