Java Comments

244 views
optmiize_your_resume_1

In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way.

In computer programming, Java comments are statements that are not executed by the compiler and interpreter. The comments 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. They are mainly used to help programmers to understand the code. For example,

// declare and initialize two variables int a =1; int b = 3; // print the output System.out.println("This is output");
Code language: JavaScript (javascript)

Here, we have used the following comments,

  • declare and initialize two variables
  • print the output

Types of Comments in Java

In Java, there are two types of comments:

  • single-line comment
  • multi-line comment

Single-line Comment

A single-line comment starts and ends in the same line. To write a single-line comment, we can use the // symbol. For example,

// "Hello, World!" program example class Main { public static void main(String[] args) { { // prints "Hello, World!" System.out.println("Hello, World!"); } }
Code language: JavaScript (javascript)

Output:

Hello, World!

Here, we have used two single-line comments:

Java Comments
  • “Hello, World!” program example
  • prints “Hello World!”

The Java compiler ignores everything from // to the end of line. Hence, it is also known as End of Line comment.

Multi-line Comment

When we want to write comments in multiple lines, we can use the multi-line comment. To write multi-line comments, we can use the /*….*/ symbol. For example,

/* This is an example of multi-line comment. * The program prints "Hello, World!" to the standard output. */ class HelloWorld { public static void main(String[] args) { { System.out.println("Hello, World!"); } }
Code language: JavaScript (javascript)

Output:

Hello, World!

Here, we have used the multi-line comment:

/* This is an example of multi-line comment. * The program prints "Hello, World!" to the standard output. */
Code language: CSS (css)

This type of comment is also known as Traditional Comment. In this type of comment, the Java compiler ignores everything from /* to */.

Why are comments important in Java?

Java Comments are used in programs to make the code more understandable. Comments in Java (remarks) make a program more intelligible as they set the details of the code. Appropriate utilization of remarks also makes support simpler and discovering bugs effectively.

One thing you should always consider that comments shouldn’t be the substitute for a way to explain poorly written code in English. You should always write well-structured and self-explaining code. And, then use comments.

Note: In most cases, always use comments to explain ‘why‘ rather than ‘how‘ and you are good to go.

Java Comments
20%off for Ink 280 281

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More