A statement is a unit of code that performs some action. In Dart, statements end with a semicolon. Semicolon are optional. There are two kinds of statements:
Simple statements (usually one line of code)
Block statements (having one or many blocks of code)
Block statements are created using curly brackets {...}
Keywords
Most statements start with a keyword. This is an English word. Some statements may use multiple words, some are optional some are mandatory.
Dart has significantly fewer keywords compared to Java. Some key facts:
Dart has 33 keywords in total as of Dart 2.16. This includes keywords like class, extends, abstract, get, set, var, etc.
In comparison, Java has around 52 keywords as of Java 15. This includes keywords like public, protected, private, final, static, etc. which Dart does not have.
Some of the reasons Dart has fewer keywords are:
Dart does not have access modifiers like public, private, protected. Everything is public by default.
Dart does not have static keyword. Classes are final by default.
Dart does not have final keyword. Variables are final by default unless declared var.
The reduced number of keywords in Dart makes the language simpler and more concise compared to Java.
So in summary, Dart has significantly fewer keywords (around 35% less) compared to Java because:
Dart omits some keywords like access modifiers, static, final that are present in Java
Things like class visibility and variable mutability are handled differently in Dart, which reduces the need for certain keywords.
This makes Dart a more minimal and concise language, while still being expressive and powerful. However, Dart borrows some concepts and syntax from Java to make it familiar for Java developers.
Assign statement
You're right, = represents an assignment statement in Dart, not an expression like in Python.
In Dart:
- An assignment uses = and is a statement. It performs an action (assigning a value).
For example:
var a = 10; // Assignment statement
- Expressions evaluate to a value. They are used in statements.
For example:
var b = a + 5; // a + 5 is an expression, = is a statement
So in Dart, = is used to assign values in a statement, it does not represent an expression itself.
In Python, = represents an expression that evaluates and assigns a value. It is used like this:
a = b = 10 # This is an expression
So in summary, the difference is:
In Dart:
\= represents an assignment statement
It is used to assign a value to a variable
In Python:
\= represents an expression that evaluates and assigns a value
It is used to both evaluate and assign a value in one step
So, = represents a statement in Dart, not an expression like it does in Python. The semantics around = are different between the two languages.
All Keywords
Here is a table of Dart keywords grouped by category with descriptions:
Category | Keyword | Description |
Classes | class | Declares a class |
extends | Used to inherit from another class | |
abstract | Declares an abstract class | |
Variables | var | Declares a variable |
final | Declares a final variable | |
Functions | Function | Declares a function |
call | Calls a function | |
Control Flow | if | Conditional statement |
else | Else block for if statement | |
for | For loop | |
while | While loop | |
do | Do-while loop | |
switch | Switch statement | |
case | Case block for switch statement | |
break | Breaks out of a loop or switch block | |
continue | Continues to next iteration of a loop | |
return | Returns a value from a function | |
Libraries | import | Imports a library |
library | Declares a library | |
part | Part of a library | |
Types | typedef | Defines an alias for a type |
dynamic | The dynamic type | |
void | The void type | |
bool | The bool type | |
int | The int type | |
double | The double type | |
num | Supertype for numbers | |
String | The String type | |
Null | The null type |
I will try to find the details for each keyword. Do not bother to learn all these keywords right now. All in good time. Learning Dart is a pleasure because it has such a concise syntax.
Disclaim: Almost the entire article is created by Rix. My contribution is very small. You can do such articles but only after you have learned several languages. However, you can ask new questions and add comments below.