Dart Comments
Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans the code of compilation. It is a good practice to include comments in your code to refer them when you get back to your code.
There are three types of comments in the Dart programming language:
- Single line comments
- Multi-line comments
- Documentation comments
Single-line comments
A single-line comment is a comment that is only of a single line. It begins with ‘//’ and ends in a line.
Syntax:
// this is a single-line comment
Example:
Consider the following code,
// This program depicts the concept of single-line comment
void main( )
{
// print( 'This is the Flutter's tutorial’ );
print( 'This is Dart tutorial' );
}
Output:
This is a Dart tutorial
As you can see, the compiler completely ignored the line “print( ‘This is a Flutter’s tutorial’)” because it is a comment, and printed the next line only.
Multi-line comments
A multi-line comment is a comment that consists of multiple lines. It begins with ‘/*’ and ends with ‘*/’. Multi-line comments can be nested.
Syntax:
/* This is a
Multi-line comment. */
Example:
Consider the following code,
// This program depicts the concept of multi-line comment
void main( )
{
var a = 4, b = 5, diff, mul;
/* diff = a - b;
mul = a * b; */
print( '\nThe value of a is $a \nThe value of b is $b \nThe value of diff is $diff \nThe value of mul is $mul' );
}
Output:
The value of a is 4
The value of b is 5
The value of diff is null
The value of mul is null
Since the lines assigning values to ‘diff’ and ‘mul’ are comment out, no value is assigned to these variables as the compiler simply ignored them. Therefore, null got printed, which is the default value assigned to the compiler in case no value is initialized.
Documentation comments
Documentation comments are multi-line or single-line comments that begin with ‘///’ or ‘/**’. We can use ‘///’ in the consecutive lines which would work same as a multi-line comment.
While analysing the code during compilation, the compiler ignores all the text except the text enclosed within the square bracket ‘[ ]’. These brackets are used as references to classes, methods, fields, functions, and parameters.
Syntax:
/// This is
/// a documentation
/// comment
Example:
Consider the following code,
// This program depicts the concept of multi-line comment
void main( )
{
var a = 4, b = 5, diff, mul;
/// diff = a - b;
/// mul = a * b;
///print( '\nThe value of a is $a \nThe value of b is $b \nThe value of diff is $diff \nThe value of mul is $mul' );
print( ' This shows documentation comment ' );
}
Output
This shows documentation comment