Comments in java. Types of comments used in java
Java Comments :
The Java comments are the 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.
Java Comments are used in programs to make the code more understandable.
Comments in Java are often are non-executable statements, used to understand the code better and to make it more readable.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1. Single Line Comment:
The single line comment is used to comment only one line.It is the easiest typed comments.
Syntax:
//This is comment
Example:
public class comment
{
public static void main(String[] args)
{
//This is main method
int x=10;
System.out.println("X="+x);
}
}
Output :
X=10
2. Multi Line Comment:
The multi line comment is used to comment multiple lines of code.
Syntax:
/*
comment start
This is multiline comment
comment end
*/
Example:
public class comment
{
public static void main(String[] args)
{
/* Here, let's declare and print a variable in java. */
int x=10;
System.out.println("X="+x);
}
}
Output :
X=10
3. Documentation Comment :
The documentation comment is used to create documentation API.
It is used by Javadoc tool while creating the documentation for the program code.
Syntax:
/**
Comment starts
This is documentation comment
This is javadoc comment
Comment end
*/
Example:
public class comment
{
/**
* This method is used to find sum of two integers
* @param a
* @param b
* @return
*/
int add(int a,int b)
{
return (a+b);
}
public static void main(String[] args)
{
comment c=new comment();
int s=c.add(5,10);
System.out.println("sum="+s);
}
}
Output :
Sum=15
Comments
Post a Comment