Java Comments


Comments

Comments are expressions or reactions or actions about random topics given by users or the general audience.
Java Comments are used for explaining the behavior or the progress of a task or a process written by other users for better understanding for future use.
Java Comments are used to prevent execution when testing alternative code and are defined in the source code for reference. Adding comments is considered good practice in Java source code.

Single line comment:

The easiest way of writing comments starts with two forward slashes (//).
Single line comment is used for commenting only one line.

Syntax:

//comments here are considered single-line comments.

Example:

//Single line Comment.
System.out.println("Single Line Comment ");
Any text that starts with // is ignored by Java and will not be executed.

Multi-line comments:

To overcome writing // on every line, this type of comment can be used.
Multi-line comments start with /* and end with */ and any text between them will be ignored by Java.

Syntax:

/*
comments here are considered
multi-line comment
*/

Example:

/* The code below will print the words ‘ This is an example of Multi-Line Comment’ to the screen */
System.out.println("Multi Line Comment ")

Documentation comments:

Documentation comments are used when writing code for package/software. The documentation comment is used for creating documentation API.
For creating Documentation API, we should use the JavaDoc tool.

Syntax:

/*
*comments here
*are considered as
*documentation comments
*/

Example:

/** The AddData class is used to for adding two numbers */
public class AddData {
/** The addNumber () method returns addition of given numbers.*/
public static int addNumber (int numberOne, int numberTwo){return numberOne+numberTwo;}
}

Example:

Com.knowledge2life public class Main { public static void main(String[] args) { // This is a comment System.out.println("Knowledge2life"); } }

OUTPUT:

Knowledge2life