The Dart language has special support for the following basic types:
//simple Dart literals
var x = 10; //integer
var h = 0xDEADBEEF; //integer
var y = 1.1; //double
var e = 1.25e3; //double
Numbers are converted implicit when possible in this direction: int->double. In next example we use an integer literal but a double literla is required. However the compiler will not complain.
void main() {
//implicit coercion
double x = 10;
print(x); // 10.0
}
For conversion in this direction: double->int we can loose some data. In this case only explicitly coercion is possible: There are functions available to do the job.
void main() {
//explicit coercion
double y = 2.25;
int a = y.floor();
int b = y.ceil();
print(a); // 2
print(b); // 3
}
Homework: Let's try this example on-line: declare-vars
A string is a sequence of code units. You can use either single or double quotes to create a string. Dart strings are encoded using Unicode UTF-16. You can use single or double quotes to create string literals.
//simple Dart strings
var message = 'Sage-Code teaches Computer Science for free!';
var why = "It's a hoby of the author.";
There is a technique often used to convert numbers into strings so that you can print the number withing a text. It is also possible to convert a string into a number but this require special attention since the string may contain something else and conversion may fail.
Number to string conversion:
// int -> String
String literalString = 101.toString(); // '101'
// double -> String
String piAsString = 3.1415.toStringAsFixed(2); // 3.14
String to number conversion:
// String -> int
var number = int.parse('101'); // 101
// String -> double
var piNumber = double.parse('3.14'); // 3.14
You can use a special notation to insert the result of an expression inside to a string. The expression can be as simple as a variable or complex like the result of a function.
You can use "$var_name" notation to include a variable value into a string:
// using variable name
var name = "Elucian";
var twitter = "@elucian_moise";
print("May name is: $name");
print("My twitter is: $twitter");
May name is: Elucian My twitter is: @elucian_moise
You can use "${expression}" notation to include result of expression into a string. This is very convenient way to create output strings.
// using toUpperCase() function
var language = "html";
print("Is: ${language.toUpperCase()} a programming language?");
Is: HTML a programming language?
Strange but you can use "+" to concatenate two strings. This is the usual method, but in Dart there are other methods to concatenate strings, as we show in next examples.
// using concatenation
void main() {
var test = '10' + 10.toString();
print(test); //1010
}
1010
Dart offer you suprises. Adjacent strings are automatically concatenated without "+" this can be useful for long strings that do not feet on a single line just in case you want to breack it. The end of line is not included in string:
// using concatenation
void main() {
var test = 'this is' " a "
'test';
print(test); //this is a test
}
A regular string support escape sequences, for example "\n" represents new line. However there is a way to create "raw" strings that are imune to escape substiturion. For this you add a small "r" before the string literal.
// using raw string
var test = r'escape sequence \n '
'do not work inside '
'raw strings.';
print(test); //
escape sequence \n do not work iside raw strings.
Pithon has introduced first time triple quoted strings. This notation is now popular among new computer languages, so it is supported in Dart.
//using triple qoted strings
void main() {
//incorrect indentation
var s1 = '''
You can create
multi-line strings like this one.
''';
print(s1);
//correct indentation
var s2 = """This is also a
multi-line string.""";
print(s2);
}
Boolean type is also known as logic type. It supports only two values: true and false. You can declare a Boolean using "var" or "bool" keywords. I mean, hold on, I lied. Boolean null is partially supported. A "bool" variable is null when not assigned. But this values are not valid in Boolean expressions.
//using Boolean
void main() {
var b1 = true;
bool b2 = false;
bool b3; //incorrect declaration
print(b1); //true
print(b2); //false
print(b3); //null (surprise)
}
Next table will help you understand better all possible combinations of values and the result yield by logical operators: "!" = NOT, "||" = OR, "&& "= AND.
P | Q | !P | P || Q | P && Q |
---|---|---|---|---|
false | false | true | false | false |
true | false | false | true | false |
false | true | true | true | false |
true | true | false | true | true |
Comparison operators like "==" or "!=" will create Boolean results based on non Boolean values. This operator can be used for: numbers, string as well as Boolean values. A less know trick is to assign result of expressions to a Boolean variable. Check this out:
//using comparison operators
void main() {
//compare numbers
bool b1 = 1 == 1;
assert (b1); // true
//compare booleans
bool b2 = true == b1;
assert (b2); // true
//compare strings
bool b3 = "this"!="that";
assert (b3); // true
//compare strings
bool b4 = "yes is winter" =="yes " "is " "winter";
assert (b4); // true
print("done.");
}
Homework: Open on-line then modify this program to demonstrate that (1 < 2) and ( 2 > 0 ): dart-comparison
Read next: Functions