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:
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:
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:
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.
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:
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!
