Dart, like all programming languages, provides basic data types that act as building blocks for coding. In this lesson, we will delve into the core data types in Dart: int
, double
, bool
, String
, and null
. We will learn how to use these data types to represent numbers, boolean values, text data, and the absence of value, respectively.
In Dart, numerical data types are used to represent numbers. Specifically, this section focuses on int
and double
. The int
data type is used to represent whole integer numbers, while the double
data type is used to represent decimal numbers — numbers with decimal points.
The largest value an int
can store is 9223372036854775807
, which is . The smallest value it can hold is -9223372036854775808
, which is . Here's an example of using the int
data type:
Dart1int daysInWeek = 7; 2print(daysInWeek); // This will print: 7 3 4int maximalInteger = 9223372036854775807; 5print(maximalInteger); // This will print: 9223372036854775807 6 7int tooBigInteger = 9223372036854775808; // Oops! Error, the number is too large
Next, let's explore the double
data type. The double
data type is used when dealing with numbers that have decimal points, also known as floating-point numbers. A double
can hold up to 15 decimal digits of precision. Consider the following example:
Dart1double pi = 3.141592653589793; 2print(pi); // This will print: 3.141592653589793
The next item in the agenda is the bool
data type.
The bool
data type in Dart can hold one of two possible values: true
or false
. This particular data type is extensively used in logical expressions and decision-making structures. Here's a simple example:
Dart1bool isEarthRound = true; 2print(isEarthRound); // This will print: true 3 4bool isEarthFlat = false; 5print(isEarthFlat); // This will print: false
Dart treats String
as a basic data type, using it to store a sequence of characters — or simply put, text. The string is always encased within either double or single quotes.
Dart1String welcome = "Welcome to Dart!"; 2print(welcome); // This will print: Welcome to Dart!
In Dart, the String
data type is immutable. Once a String
value is created, its content cannot be changed; however, the variable referencing the String
can be reassigned to point to a different String
value.
As we conclude this journey, we discuss a very special value: the null
value. null
signifies "no value", "nothing", or "unknown". It is crucial to note that it's not equivalent to an empty string (""
) or 0.
Here's an example of how to assign null
to a variable:
Dart1String unknown = null; 2print(unknown); // This will print: null
Bravo! You've successfully navigated Dart's basic data types. You are now equipped to use int
and double
for numerical computations, bool
for decision-making, String
to handle text, and null
to represent an unknown value.
Although we've covered a substantial amount of information, further practice awaits. This subsequent practice aims to solidify your understanding. Therefore, prepare for the exercises designed to test your newly acquired knowledge!