Java Comments
Comments
Why Do We Use Java Comments?
Types of Java Comments
Single-line comments
Multi-Line Comments
Documentation Comments
Important components
Java Comments
Java comments are notes in the source code of a Java program that are ignored by the
compiler. They are used to provide explanations, descriptions, or any relevant information
about the code. It makes it easier to understand for humans.
Comments, as they provide valuable context and explanations about the code, making it
easier for both the original developers and others who work with the code to understand its purpose
and functionality.
Why Do We Use Java Comments?
They help explain complex logic.
Useful for generating documentation and guiding future developers.
Enable temporarily disabling code sections for testing and debugging purposes.
Types of Java Comments
Three types of comments in Java
Single-Line Comments
Multi-Line Comments
Documentation Comments
Single-line comments
Single-line comments in Java are used to comment out a single line or a part of it. They
begin with two forward slashes (//) and extend to the end of the line. Single-line comments are
helpful for brief explanations or notes relevant to the local code.
Syntax
// comment text
Example : Single-line comments
public class Main {
public static void main(String[] args) {
// Declare a variable to store the number
int number = 10;
// Calculate the square of the number
int square = number * number;
// Print the result
System.out.println("The square of " + number + " is: " + square);
}
}
Example : Single-line comments
public class Main {
public static void main(String[] args) {
System.out.println("Hello World"); // This is a comment
}
}
Multi-Line Comments
Multi-line comments in Java are used to comment out blocks of text spanning multiple lines.
They start with /* and end with */. Everything between these markers is considered a comment and
is ignored by the Java compiler. Multi-line comments help provide detailed explanations or
temporarily disable a block of code during debugging.
Syntax
/*
This is a multi-line comment.
It can span several lines.
The Java compiler ignores everything in this block.
*/
Example : Multi Line Comments
public class Main {
public static void main(String[] args) {
int n = 10; // number of elements in the Fibonacci series
/* The Fibonacci sequence is a series of numbers where
a number is the addition of the last two numbers,
starting with 0, and 1.
This loop calculates each number in the series
up to the nth number and prints them. */
int n1 = 0, n2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i) {
System.out.print(n1);
if (i < n) {
System.out.print(", ");
}
// compute the next term
int sum = n1 + n2;
n1 = n2;
n2 = sum;
}
}
}
Documentation Comments
Documentation comments in Java, often referred to as doc comments, are a special type of
comment used to generate JavaDoc documentation. JavaDoc is a tool provided by Oracle that
generates standard HTML documentation from these comments. Documentation comments are
crucial for explaining the code’s functionality, parameters, return values, and thrown exceptions at a
higher level. It makes them an essential part of professional Java development.
Syntax
/**
* Description or summary of the class, method, or field.
*
* @tag description
*/
Important components
Start and End: The comment begins with /** and ends with */. This distinguishes it from regular
multi-line comments that start with /*.
Description: The first part is usually a description or summary of the class, method, or field that the
comment is documenting.
Tags: After the description, you can include various tags to provide more specific information.
Various tags used in Java documentation comments are as follows.
Tag Name Description Syntax Example
@param Describes a parameter passed to a method @param paramName
or constructor. description
@return Describes the return value of methods. @return description
@throws/ Describes the exception thrown by a @throws ExceptionClass
@exception method. description
@see Adds a reference or a link to another @see className#methodName
element.
@author States the author of the code. @author name
@version Specifies the version of the class, interface, @version versionInfo
or method.
@since Indicates when the class, method, or field @since version
was first added to the project.
@deprecated Marks the method or class as obsolete. @deprecated description
@link Inserts an inline link to another element in {@link package.class#member
the documentation. label}
@linkplain Like @link, but displays the link text {@linkplain
instead of the code font. package.class#member label}
@code Displays text in code font. {@code code}
@literal Indicates that text should be interpreted {@literal text}
literally, ignoring Javadoc tags.
Example : Documentation Comments
/**
* The BankAccount class simulates a bank account.
*/
public class BankAccount {
/**
* The balance field stores the current balance.
*/
private double balance;
/**
* The constructor initializes the balance to 0.
*/
public BankAccount() {
balance = 0;
}
/**
* The deposit method adds money to the account.
*
* @param amount The amount to add to the balance.
*/
public void deposit(double amount) {
balance += amount;
}
/**
* The withdraw method subtracts money from the account.
*
* @param amount The amount to subtract from the balance.
*/
public void withdraw(double amount) {
balance -= amount;
}
/**
* The getBalance method returns the account balance.
*
* @return The current balance of the account.
*/
public double getBalance() {
return balance;
}
/**
* The main method for the BankAccount class.
*
* @param args Command line arguments (not used).
*/
public static void main(String[] args) {
BankAccount myAccount = new BankAccount();
// Demonstrate deposit
myAccount.deposit(200.00);
System.out.println("Balance after deposit: " + myAccount.getBalance());
// Demonstrate withdrawal
myAccount.withdraw(50.00);
System.out.println("Balance after withdrawal: " + myAccount.getBalance());
}
}