×

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:

  1. Single line comments
  2. Multi-line comments
  3. 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

Related Topics

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.