Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What are Comments in Java? – Know its Types

Published on Aug 19,2019 479 Views
3 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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?

Java - Comments in Java - EdurekaComments 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 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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.