What are comments in Java language?



In general comments in any programming language helps make the source code readable, compiler neglects these comments.

Java supports three types of comments –

  • Single line comments − Using these you can comment single lines. The compiler ignores everything from // to the end of the line.

  • Multiline comments − Using these you can comment multiple lines. The compiler ignores everything from /* to */.

  • Documentation comments − This is a documentation comment and in general it’s called doc comment. The JDK Javadoc tool uses doc comments when preparing automatically generated documentation.

Example

Following example demonstrates the usage of all the three types of comments in Java.

Live Demo

/**
   * @author Tutorialspoint
*/ 
public class CommentsExample {
   /*
      Following is the main method here,
      We create a variable named num.
      And, print its value    
   * */
   public static void main(String args[]) {
      // Declaring a variable named num
      int num = 1;
      // Printing the value of the variable num
      System.out.println("value if the variable num: "+num);
   }
}

Output

value if the variable num: 1

Advertisements