Dart Programming - Data Types
One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.
The Dart language supports the following types−
- Numbers
- Strings
- Booleans
- Lists
- Maps
Numbers
Numbers in Dart are used to represent numeric literals. The Number Dart come in two flavours −
Integer − Integer values represent non-fractional values, i.e., numeric values without a decimal point. For example, the value "10" is an integer. Integer literals are represented using the int keyword.
Double − Dart also supports fractional numeric values i.e. values with decimal points. The Double data type in Dart represents a 64-bit (double-precision) floating-point number. For example, the value "10.10". The keyword double is used to represent floating point literals.
Strings
Strings represent a sequence of characters. For instance, if you were to store some data like name, address etc. the string data type should be used. A Dart string is a sequence of UTF-16 code units. Runes are used to represent a sequence of UTF-32 code units.
The keyword String is used to represent string literals. String values are embedded in either single or double quotes.
Boolean
The Boolean data type represents Boolean values true and false. Dart uses the bool keyword to represent a Boolean value.
List and Map
The data types list and map are used to represent a collection of objects. A List is an ordered group of objects. The List data type in Dart is synonymous to the concept of an array in other programming languages. The Map data type represents a set of values as key-value pairs. The dart: core library enables creation and manipulation of these collections through the predefined List and Map classes respectively.
The Dynamic Type
Dart is an optionally typed language. If the type of a variable is not explicitly specified, the variable’s type is dynamic. The dynamic keyword can also be used as a type annotation explicitly.
A variable is “a named space in the memory” that stores values. In other words, it acts a container for values in a program. Variable names are called identifiers. Following are the naming rules for an identifier −
Identifiers cannot be keywords.
Identifiers can contain alphabets and numbers.
Identifiers cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.
Variable names cannot begin with a number.
Type Syntax
A variable must be declared before it is used. Dart uses the var keyword to achieve the same. The syntax for declaring a variable is as given below −
var name = 'Smith';
All variables in dart store a reference to the value rather than containing the value. The variable called name contains a reference to a String object with a value of “Smith”.
Dart supports type-checking by prefixing the variable name with the data type. Type-checking ensures that a variable holds only data specific to a data type. The syntax for the same is given below −
String name = 'Smith'; int num = 10;
Consider the following example −
void main() { String name = 1; }
The above snippet will result in a warning since the value assigned to the variable doesn’t match the variable’s data type.
Output
Warning: A value of type 'String' cannot be assigned to a variable of type 'int'
All uninitialized variables have an initial value of null. This is because Dart considers all values as objects. The following example illustrates the same −
void main() { int num; print(num); }
Output
Null
The dynamic keyword
Variables declared without a static type are implicitly declared as dynamic. Variables can be also declared using the dynamic keyword in place of the var keyword.
The following example illustrates the same.
void main() { dynamic x = "tom"; print(x); }
Output
tom
Final and Const
The final and const keyword are used to declare constants. Dart prevents modifying the values of a variable declared using the final or const keyword. These keywords can be used in conjunction with the variable’s data type or instead of the var keyword.
The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final.
Syntax: final Keyword
final variable_name
OR
final data_type variable_name
Syntax: const Keyword
const variable_name
OR
const data_type variable_name
Example – final Keyword
void main() { final val1 = 12; print(val1); }
Output
12
Example – const Keyword
void main() { const pi = 3.14; const area = pi*12*12; print("The output is ${area}"); }
The above example declares two constants, pi and area, using the const keyword. The area variable’s value is a compile-time constant.
Output
The output is 452.15999999999997
Note − Only const variables can be used to compute a compile time constant. Compile-time constants are constants whose values will be determined at compile time
Example
Dart throws an exception if an attempt is made to modify variables declared with the final or const keyword. The example given below illustrates the same −
void main() { final v1 = 12; const v2 = 13; v2 = 12; }
The code given above will throw the following error as output −
Unhandled exception: cannot assign to final variable 'v2='. NoSuchMethodError: cannot assign to final variable 'v2=' #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:178) #1 main (file: Test.dart:5:3) #2 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261) #3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
Comments
Post a Comment