Comments in Java

Java compiler ignores all the characters inside a comment. It improves the readability of your source code. There are two types of comments-

  1. Single-Line Comment
  2. Multi-Line Comment

Single-Line Comments in Java

It is also known as end-of-line comments. It starts with // and goes till the end of a line. Whatever you write between // and the end of the line is considered as a comment.

Syntax

//I am a single line comment

Example

class SingleLineComment
{
	public static void main(String args[])
	{
		int noOfBalls = 6; //this variable stores the number of balls in an over
		System.out.println("An over has " + noOfBalls + " balls");
	}
}

Output

An over has 6 balls

Multi-Line Comments in Java

It starts with /* and ends with */. It spans multiple lines of code.

Synatx

/* 
I  
am
a
multi-line  
comment 
*/

Example

class MultiLineComment
{
	public static void main(String args[])
	{
		/*
			The below loop will
			print the table of 2. 
		*/
		for(int i=1; i<=10; i++)
		{
			System.out.println(2*i);
		}
	}
}

Output

2
4
6
8
10
12
14
16
18
20