How to include comments in java program?
- A comment describes or explains the operation of the java program to anyone who is reading its source code.
- Contents of comment are ignored by the compiler.
What are the types of comments in java?
Three types of comments in java,
- Single line comment
- Multiline comment
- Documentation comment
Java Single line comment
Single-line comment begins with a // and ends at the end of the line, this comment symbol is used to comment only a particular line.
//This method is used to sum the integers.
Java Multiline comment
Multiline comment begins with /* and ends with */, Anything between two comment symbols is ignored by compiler and several lines can be commented using this comment symbols.
/*
This is a simple java program.
File name is "Example.java".
*/
Java Documentation comment
Documentation comment begins with a /** and ends with a */, JDK javadoc tool uses doc comments when preparing automatically generated documentation.
Sample Java program with different types of comments.
/**
* This program is used to find the sum of two integers
* variables and displays the result.
*
* Author: Test user
* Date: 20-12-2008
* Module: Test Module
*
*/
class Example {
public static void main(String args[]) {
int a = 10;
int b = 20;
int sum;
/* Computes sum of 'a' and 'b' integers
variables.
*/
sum = a + b;
// prints result
System.out.println("sum of " + a +" and " + b + ":" + sum);
}
}
Output:
sum of 10 and 20:30
« Previous
Next »
Java Programming Language Blog