What are Comments in Java? – Know its Types

Last updated on Jun 17,2021 13.2K Views

What are Comments in Java? – Know its Types

edureka.co

Comments in Java are the statements that are not executed by the compiler and interpreter. It 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 a specific time. Let’s understand all about Java Comments through the medium of this article.

Below topics are covered in this article:

Let’s get started!

What are comments in Java?

Comments in Java are often are non-executable statements, used to understand the code better and to make it more readable. As mentioned above, it can also be helpful in preventing code execution when testing an alternative code. In short, Java comments are non-executable statements, used to understand Code better.

Now let’s move further and know the various types of comments in Java.

Types of Comments

Basically, there are three types of comments in Java. They are as follows:

Now let’s get into the details of these comments.

1. Single-line Comments

This type of comment is mainly used for describing the code functionality. It is the easiest typed comments. Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java implies it will not be executed. Now, let’s take a small example to know how single-line comments can be used.

public class Example1 {
public static void main(String[] args) {
int u=100; //Here, u is a variable
System.out.println(u); // Prints the value of the variable u
}
}

You can see in the above example that I have used single-line comments to comment on the specific lines in a program. That’s how it works. Now, let’s understand what are multi-line comments.

2. Multi-line Comments

To describe a full method in a code or a complex snippet single line comments can be tedious to write since we have to give ‘//’ at every line. So to overcome this multi-line comments can be used. Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by Java. It is used to comment multiple lines of code.

Now, let’s take a small example to know how multi-line comments can be used.

public class Example2 {
public static void main(String[] args) {
/* Here, let's declare and print
 a variable in java. */
int u=100;
System.out.println(u);
}
}

You can see in the above example I have used multi-line comments to comment on the lines in a program. That’s how it works. Now, let’s understand what is documentation comments.

3. Documentation Comments

This type of comments is used generally when you are writing code for a project/ software package. It helps you to generate a documentation page for reference, which can be used for getting information about methods present, its parameters, etc. To know more, refer to this link.

This brings us to the end of this article on Comments in Java. I hope you found it informative.

Check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this “Comments in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework