Java Comments

Java comments stop execution of code by the compiler and interpreter.

Why should we use comments in a code?

  • Comments are used to to add the details of the code in program.
  • It can also be used to prevent the execution of program code while testing the alternative code.

 

In Java there are three types of comments: 

  1. Single-line comments.
  2. Multi-line comments.
  3. Documentation comments.

Single Line Comment(//)

The single-line comment is used to comment only one line of the code.  

Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java.

Example,

// Describe information or explanation about the variable, method, class, or any statement.

System.out.println("Learn java tutorials");

 

Multi-line comments(/* and */).

The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet or to comment multiple lines of code at a time. It will be difficult to use single-line comments there.

Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java.

Example,

/*
The code below will print the words Learn java tutorials
System is a class.
out is an object of the PrintStream class.
println() is a method of the PrintStream class.
*/

System.out.println("Learn java tutorials");

 

Documentation Comments (/** and */)

Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods, arguments, etc., are used in the code.

We need to use the javadoc tool to create documentation API. The documentation comments are placed between /** and */.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

96033