Comments in Java

Notes:

Comments in Java Programming Language:

Comments are something like notes in the code.

Comments are used to explain the code logic,
so that we can be able to understand the code later
as well as
other developers can also be able to read and understand the code easily.
Comments are used to increase the readability and understandability of the source code.

Comments are used to document the source code. (Documentation)

Note:
Comments are ignored by the Java compiler.

Types of comments in Java:
There are 3 types of comments in Java

Single line comment: (// comment goes here)
- Anything is written after // symbols in the line is treated as a comment
- // are used to write single line comments

Multiline comment: (/* comment goes here */)
- Anything is written in between /* and */ is treated as a comment
- /* and */ are used to write multiple lines of comments

Documentation comment: (/** comment goes here */)
- Anything is written in between /** and */ is treated as a comment
- /** and */ are used to document a class, interface etc. i.e. any user defined data type; which gives an extra information to the user, so that he or she can use it easily

Note: If we don’t want to execute some part(s) of the code, we can comment them so that they can be ignored by the compiler.

Example Code:

package commentsdemo;

/**
* Comments Demo is the main class for this project
* @author Manjunath Chidre
* @version 1.0
*/
public class CommentsDemo {

public static void main(String[] args) {

// println is a method; it is a member of out object
// out is an object; it is a member of System class

/*
println is a method; it is a member of out object
out is an object; it is a member of System class
dot is a member access operator; it is used to access members of an object, class etc.
*/

System.out.println("Hello world");

Add(5,5);

}

/**
* Add method displays sum of 2 integer numbers
* @param num1 Enter an integer number
* @param num2 Enter an integer number
* @return Nothing
*/
public static void Add(int num1,int num2)
{
System.out.println(num1 + num2);
}
}