Java Comments

Java Comments are used for the various purpose:

  • Makes programs code readable.
  • Used for Java code explanation.
  • Used to prevent the execution by the compiler and interpreter.

For example, if you run this Program:

public class Study4Geeks {
	public static void main(String[] args){
		int a=5;
		int b=10;
		int c=20;
		System.out.println("sum is = "+(a+b));
		//System.out.println(c);
		}
}

output:

sum is = 15

Here, the only sum is = 15 printed but not printed the value of C because of System.out.println(c); in under the comment.

Types of Java Comments

There are three types of comments.

  1. Single Line Comment
  2. Multi-Line Comment
  3. Documentation Comment
java-comments

Single Line Comment

The single line comment is used to comment on only one line. It starts with two forward slashes //.

Example:

public class Study4Geeks {
	public static void main(String[] args){
		int number=510; // Integer type variable
		System.out.println("number is = "+(number));
		}
}

output:

number is = 510

Multi-Line Comment

  • Used to comment multiple lines of code.
  • If you uses Multi-line comment then you have to placed all line of code or text between /* and */ .

Example:

public class Study4Geeks {
	public static void main(String[] args){
		/* here, two output is print
		  1. Study4Geeks
		  2. Online Learning Portal
		 */
    System.out.println("Study4Geeks");
    System.out.println("Online Learning Portal");
		}
}

output:

Study4Geeks
Online Learning Portal

Documentation Comment

It is used for creating documentation in a program.

Example:

public class Study4Geeks {
	public static void main(String[] args){
		/** here a and b is integer type variable.
		    First print statement print the Sum of a and b.
		    second print statement print the Multiplication of a and b
		 */
		int a=10;
		int b=20;
    System.out.println("Sum of a and b is "+(a+b));
    System.out.println("Multiplication of a and b is "+(a*b));
		}
}

output:

Sum of a and b is 30
Multiplication of a and b is 200
Do you know?
Java Comments This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.