Output and variables
Learning objectives
- You practice the basics of programming with Dart.
- You know basics of variables in Dart.
In Dart, every program has a main function that acts as the entry point to the program, i.e. when you run a Dart program, the Dart runtime calls the main function. This main function is called main
. The following example outlines the classic "Hello world!" program written in Dart.
Semicolons
Dart uses semicolons as statement terminators, similar to e.g. Java and C.
Finals and variables
The key thing to remember about variables in Dart is that every value is an object. Each value -- that is an object -- is instantiated from a class, which defines the type of the value. Every value is associated with a type. That is, Dart is a strongly-typed language.
Dart is a strongly-typed language.
The following example outlines a program with two variables that are assigned values and then printed using the print
statement. As the values of the variables do not change within the program once they have been set, the variables are declared as final
.
In the above example, the variable types are automatically inferred based on the values. The convention is to let Dart automatically infer the types of local variables, i.e. variables defined within functions, while explicit type annotations should be used when defining non-local variables (e.g. function parameters and class variables).
Let Dart automatically infer the types of local variables. Otherwise, explicitly define variable types.
If we use a variable so that we expect to reassign its value in the program, we use the keyword var
instead of final
. In the following example, we create a variable called name
and assign its value. Then, later on in the program, we reassign the value.
When using var
(and final
in the previous examples) to instantiate a variable, the type of the variable is determined by Dart. Once a variable has a type, the type cannot change. That is, Dart is statically typed.
Dart is statically typed.
For example, the following application will not work. When you attempt to run or compile the program, you will see an error stating that a value of type 'int' cannot be assigned to a variable of type 'String'.
We can check the runtime type of the variables using the runtimeType
property. Note that the value of this property is platform specific and should be used only for debugging purposes. As an example, if you run your Dart programs transpiled into JavaScript, the types for some of the variables differ from the types on the native platform. In the following example, we will see the runtime types of two variables.
Commonly used variable types
Dart has commonly used variable types present in other programming languages as well. These include the often used int
, double
, and String
.
String interpolation
The above example also demonstrates how string interpolation in Dart works. String interpolation refers to the process of injecting variable values into strings. In practice, in Dart, to replace a placeholder in a string with a value of a variable, use the dollar prefix $
followed by the name of the variable -- e.g. $variable
, if variable
was the name of the variable that one would use.
Internally, when performing string interpolation, Dart calls a toString
method of the objects that are to be placed into the string.
Evaluating expressions can be done also as a part of String interpolation. For example, in the following program, the variable outcome
would contain a string with the value "6".