×

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 Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 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 Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 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.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 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.

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 Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes 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 Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.