Comments in Java are statements used to explain a piece of code and are non-executable. They help improve the readability of the code and assist developers in understanding its functionality. Properly commented code enhances maintainability and makes collaboration easier. Java supports three types of comments: Single-line comments, Multi-line comments, and Documentation comments.
Single-line comments begin with //
and are used to describe a specific line of code. The text following //
until the end of the line is ignored by the compiler.
Example:
import java.util.*; // Importing Scanner class
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming");
Scanner s = new Scanner(System.in); // Creating Scanner object for input
int a = s.nextInt(); // Accepting integer input from user
s.close(); // Closing Scanner object
}
}
Multi-line comments start with /*
and end with */
. They are used to explain a section of code or a complex snippet that requires detailed explanations.
Example:
import java.util.*; // Importing Scanner class
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming");
Scanner s = new Scanner(System.in);
/*
* Accepting an integer input from the user
* and storing it in variable 'a'.
*/
int a = s.nextInt();
s.close(); // Closing Scanner object
}
}
Documentation comments start with /**
and end with */
. They are used to generate API documentation using the javadoc
tool. These comments describe methods, classes, and variables in detail, making them useful for large-scale applications.
Example:
/**
* This program demonstrates the Hello World output.
* @author Java Developer
* @version 1.0
* @since 2024-02-28
*/
public class Main {
/**
* Main method to print "Hello World".
* @param args Command-line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!"); // Printing Hello World message
}
}
Comments are essential in Java programming as they enhance code readability, provide explanations, and help in debugging. Using single-line, multi-line, and documentation comments effectively ensures that the code is understandable and maintainable over time.
Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let us have a look at what you will get with this Java training package:
You need to pay a nominal fee.
You can choose any Java certification, as per your skills and interest.
You have the option to select from online and classroom training.
A chance to study at one of the best Java training institutes in India
We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.
Naresh I technologies cater to one of the best Java training in India.
And a lot more is waiting for you.
Contact us anytime for your complete Java online training.
Course :