Keywords
Keywords refer to certain reserved words that often have a specified meaning. We use it to represent basic syntax that the compiler will understand.
Some keywords are listed below:
- Import
- Else
- As
- Abstract
- Void
- Null
- New
- Return
- Typedef
-
set
The Dart language in the program exclusively reserves several other words.
Identifiers
The identifier is the name of a variable that the compiler can read. There are specific rules for describing identifiers. The rules are as follows:
- You cannot use special characters except _ and $.
- It cannot be a reserved keyword.
- There must be no space between the variable names.
- It is case-sensitive.
Variables
The variable is the name given to the memory location declared by writing the variable type _ name and then initialized as follows:
"variable_type name = value;".
The dart language has the following built-in types:
- String
- Numbers
- Boolean
- Collections
Let us now discuss these types of variables one by one.
Strings
Dart string is a collection of characters and characters. You can create strings using single or double-quotes.
We can use single quotes or double quotes for multi-line strings. We can specify the value of a character in the string by running $ {expression}.
Example:
var name = ‘Coding Ninjas’;
In this example, the variable is called the name, and the string value is Coding Ninjas.
Numbers
We can use both int and double to represent numbers. We use these data types to represent arbitrary numbers and even decimals. The num type works with simple algebraic operators.
Booleans
We use the Dart Boolean type to test whether the statement is true or false. True and False are the two types of boolean values. Both these boolean values are compile-time constants.
Collections
A collection of Dart is a collection of similar and dissimilar data types. We use them as data structures.
We can access each element one by one. Collections include data structures like lists.
Operators in Dart
Dart contains all the usual operators that we have already come across in other languages such as C, Swift, and Kotlin.
The different types of operators in Dart can be classified as follows:
- Arithmetic Operator
- Equality Operator
- Increment and decrement Operator
- Comparison Operator
- Logical Operators
Arithmetic operators
As the name suggests, Arithmetic operators help us perform the basic arithmetic operations, including addition, subtraction, multiplication, and division.
Equality Operators
These operators help us in identifying similar and non-similar values. We can use the == operator to denote equality and != operator to denote not equals.
Comparison Operators
The comparison operators are of the following types:
- Less than (<)
- Greater than (>)
- Equal to (=)
Logical Operators
The logical OR and Logical And are also used in Dart. They help us to analyze the logical relationship between the two operands.
Comments in Dart
Comments are snippets of code that the compiler cannot read. Comments help us to write our code efficiently. Comments make the code readable and easier to understand.
There are three types of Comments in Dart:
Single-Line Comments
A single-line comment is specified with a '//' (double forward slash) that extends up to the newline character. This can be used to comment everything out until a line break is reached.
Let’s understand single-line comments in dart better with an example program.
Code:
void main()
{
// It is a single-line comment that will print Hello Ninja
print("Hello Ninja");
}
Output:
Hello Ninja
Multi-Line Comments
If you want to remark multiple lines, use the /* and */ characters; everything between /* and */ is disregarded by the compiler.
Let’s understand multi-line comments in dart better with an example program.
Code:
void main()
{
/*
* It is a
* multi-line comment program
* that will
* print Hello Ninja
*/
print("Hello Ninja");
}
Output:
Hello Ninja
Dart Documentation Comments
This is a unique form of comment that is mostly used to create documentation or a reference for a project or software package. Documentation comments begin with / or /** and can be multi-line or single-line. It has the same effect as a multi-line doc comment when used on consecutive lines. The Dart compiler ignores all text inside a documentation comment except for content wrapped in brackets. Classes, methods, fields, top-level variables, functions, and arguments are denoted by brackets. The names in brackets are resolved in the described program element's lexical scope.
Let’s understand dart documentation comments in dart better with an example program
Code:
/// This
/// is an
/// example of documentation
/// comment
Execution of Dart Programs
After writing the entire program as per all its specifications, we need to learn how to execute the program such that we can use our program to get the desired results.
We can run Dart programs in two ways-
- Command Line
- Dart Console Application
Frequently Asked Questions
-
What does Google use Dart for?
Dart is a client-oriented programming language developed by Google. It is created for rapid app development for various platforms. It is an object-oriented, open-source, general-purpose programming language.
-
What are the various data types in Dart?
The various dart data types are as follows:
Strings
Integer
Booleans
Collections like Lists
-
What is the use of typedef in Dart?
Typedef in Dart is used to give a user-defined identity to a function.
Conclusion
In this article, we have extensively discussed the Dart Syntaxes, saw how to write the hello world program, and read about keywords, operators, and comments in Dart. You can also read the blog Dart Data Types on the Coding Ninjas Website to learn about various data types available in the Dart programming language.
We hope that this blog has helped you enhance your knowledge regarding Dart Syntax and Comments knowledge. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow.
Happy Coding!