Google Dart programming syntax and basic type Learning tutorial

Source: Internet
Author: User

1. Variable Declaration

How to define variables

Copy codeThe Code is as follows:
Var name = 'bob ';

Initial Value of a Variable

Copy codeThe Code is as follows:
Int lineCount;
Assert (lineCount = null); // Variables (even numbers) are initially null.

You can use var or directly specify the type.
Final: a variable defined as final. The value cannot be changed.

Copy codeThe Code is as follows:
Final name = 'bob'; // Or: final String name = 'bob ';
Name = 'Alice '; // ERROR

2. Basic Types

String

Single or double quotation marks can be used for strings.
Copy codeThe Code is as follows:
Var s1 = 'single quotes work well for string literals .';
Var s2 = "Double quotes work just as well .";


You can directly apply the value $ {expression} in the string. If it is only a variable, you can remove {}
Copy codeThe Code is as follows:
Var s = 'string interpolation ';
Assert ('dart has $ s, which is very handy. '=
'Dart has string interpolation, which is very handy .');
Assert ('That deserves all caps. $ {s. toUpperCase ()} is very handy! '=
'That deserves all caps. string interpolation is very handy! ');


Multi-line strings are considered to be concatenated by default.
Copy codeThe Code is as follows:
Var s = 'string' 'concatenation'
"Works even over line breaks .";
Assert (s = 'string concatenation works even over line breaks .');

If you want to use a multi-line string, you can use '''
Copy codeThe Code is as follows:
Var s1 = '''
You can create
Multi-line strings like this one.
''';


Create a character string without escaping
Copy codeThe Code is as follows:
Var s = @ "In a raw string, even \ n isn' t special .";

StringBuffer, very similar to. net.
Copy codeThe Code is as follows:
Var sb = new StringBuffer ();

Sb. add ("Use a StringBuffer ");
Sb. addAll (["for", "efficient", "string", "creation"]);
Sb. add ("if you are"). add ("building lots of strings .");

Var fullString = sb. toString ();

Number

There are two types: int and double. Both of them inherit the num type.

Conversion between numbers and strings

Copy codeThe Code is as follows:
// String-> int
Var one = Math. parseInt ("1 ");
Assert (one = 1 );

// String-> double
Varonepointone = Math. parseDouble ("1.1 ");
Assert (onePointOne = 1.1 );

// Int-> String
Var oneAsString = 1. toString ();
Assert (oneAsString = "1 ");

// Double-> String
Var piAsString = 3.14159.toStringAsFixed (2 );
Assert (piAsString = "3.14 ");


Boolean Type

Bool, unlike js, is false if it is not true.

Lists (can be used as an array)

Copy codeThe Code is as follows:
Var list = [1, 2, 3]; // instantiate a list
List. add (4); // add an element 4

You can use for, for... in, foreach () to traverse a list.
Copy codeThe Code is as follows:
Var list = [1, 2, 3];
For (final x in list ){
Print (x );
}


Or

Copy codeThe Code is as follows:
Var list = [1, 2, 3];
List. forEach (element) => print (element ));

Maps (Dictionary type)

Copy codeThe Code is as follows:
Var gifts = {// A map literal
// Keys Values
"First": "partridge ",
"Second": "turtledoves ",
"Th": "golden rings "};

Gifts ["third"] = "apple"; // Add

Use foreach to traverse
Copy codeThe Code is as follows:
Var gifts = {
"First": "partridge ",
"Second": "turtledoves ",
"Th": "golden rings "};
Gifts. forEach (k, v) => print ('$ k: $ V '));

GetKeys () and getValues () Methods
Copy codeThe Code is as follows:
Var gifts = {"first": "partridge", "second": "turtledoves "};
Var values = gifts. getValues ();

// Print partridge and turtledoves, but not necessarily in that order.
Values. forEach (v) => print (v ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.