Dart basic syntax variable


This section mainly through and JavaIn order to understand the differences between the two languages and make a comparison DartLanguage grammar learning.
First try the most classicHello World

void main() {
  print("Hello World");


There are two points about variable recording

  • ObjectvarAnddynamicUse of
  • distinguishfinalAndconstTwo keywords
Object, VaR and dynamic
  1. A variable is a reference. The default value of an uninitialized variable isnull
  2. ObjectLike Java, it is the parent of all classes,ObjectDeclared variables can be of any type

    1. varIt’s modifying variableskeyword, the declared variable determines its type at the moment of assignment, and the value cannot be modified
  3. dynamicIt’s modifying variableskeyword. The actual type is not determined at compile time, but at run time.dynamicDeclared variable behavior andObjectequally
  4. When declaring variables, you can choose to add a specific type: int x = 20
  5. For local variables, according to the current dart code style, use var modification
void main() {
  Object i = 5;
  i = "Juice";
  var j = "Juice";
  // j=10; It is not allowed to modify the value of J. it has been determined that it is a string type. Compilation error
  var k;// In this case, K is considered to be of object type at compile time, so the following two operations are true
  k = "Juice";
  k = 100;
  dynamic z = "Juice"; // At run time, dynamically determine what type Z is
  z = 8;
//Parameter a is a dynamic implicit modifier
void test(a) {}
The difference between final and Const
  1. finalIs a runtime constant. A final variable can only be set once
  2. constIs a compiler constant whose value is determined at compile time to make the code run more efficiently
  3. The difference between the two is as follows:const A variable is a compile time constant,finalVariables are initialized on first use.
void main() {
  //Runtime constant
  final int i = 3;
  final j = 3;
  const k = 5;
   //const q=i;
  //Since I is a constant determined by the runtime, it cannot be assigned to Q
  //But you can assign K to it
 const q = k;
  1. Class can befinalBut it can’t beconst. IfconstVariables in a class need to be defined asstatic constStatic constant (static and Java)

Dart basic syntax  variable