Comments in Java

The java comments are statements that are not executed by the compiler and interpreter. The comments can be used to provide information or explanation about the variable, method, class or any statement. It can also be used to hide program code for specific time.

Three types of comment in Java

1. Single line comment

Syntax

// This is single line comment
// This line won't be executed

Program

 
public class SingleLineComment
{
public static void main(String args[])
{
// break keyword example
for(int i=1; i<=10; i++)
{
if(i==6)
{
break;
}
System.out.println(i);
}
}
}

Output

 
1
2
3
4
5

2. Multi line comment

Syntax

/* This is multi
line commment
This line won't
be executed */

Program

 
public class MultiLineComment
{
public static void main(String args[])
{
for(int i=1; i<=10; i++)
{
/* Exmple of
break
keyword */
if(i==6)
{
break;
}
System.out.println(i);
}
}
}

Output

 
1
2
3
4
5

3. Documentation comment

Syntax

/** This is
documentation
comment */

Program

 
public class DocComment
{
public static void main(String args[])
{
}
/** The add() method returns addition of given numbers.*/
public static int add(int a, int b)
{
return a+b;
}
/** The div() method returns divison of given numbers.*/
public static int div(int a, int b)
{
return a/b;
}
}

Output


javac DocComment.java

javadoc DocComment.java

Explanation

The documentation comment is used to create documentation API. To create documentation API, you need to use javadoc tool. Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML files and see the explanation of Calculator class provided through documentation comment.

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword