How to add different comments in the Java code?

JavaObject Oriented ProgrammingProgramming

The Java comments are statements that are not executed by the compiler and interpreter. The comments can be used to provide information about the variable, method, class or any statement. It can also be used to hide program code for a specific time.

Types of Java Comments

There are three types of comments in Java.

  • Single line comment
  • Multi-line comment
  • Documentation comment

Single line Comment

Single line comment begins with // and ends at the end of the line.

Example 1

public class SingleLineComment {
   public static void main(String[] args) {
      // To print output in the console
      System.out.println("Welcome to Tutorials Point");
   }
}

Output

Welcome to Tutorials Point

Multi-Line Comment

Multi-line comment begins with /* and ends with */ that spans multiple lines.

Example 2

public class MultiLineComment {
   public static void main(String[] args) {
      /* To print the output as Welcome to Tutorials Point
         in the console.
      */
      System.out.println("Welcome to Tutorials Point");
   }
}

Output

Welcome to Tutorials Point

Documentation Comment

Documentation style comments begin with /** and terminate with */ and that spans multiple lines. Documentation comments must come immediately before a class or an interface or a method or a field definition.

Example 3

/**
   This is a documentation comment.
   This class is written to show the use of documentation comment in Java.
   This program displays the text "Welcome to Tutorials Point" in the console.
*/
public class DocumentationComment {
   public static void main(String args[]) {
      System.out.println("Welcome to Tutorials Point");
   }
}

Output

Welcome to Tutorials Point
raja
Published on 30-May-2019 14:29:48
Advertisements