Java Comments

In this article, we will explore the different types of Java comments, their syntax, and best practices for using comments effectively in Java programming.

Java Comments – What are they?

In Java, comments are statements in a program that the compiler and interpreter don’t execute.

You can use comments to explain Java code and make it easier to read. Test alternative code with it to prevent execution.

Java Comments Types

Java comments can be divided into two types.

  1. Comment in a single line.
  2. The multi-line comment.


Java Single Line Comments

The easiest and most widely used method of commenting is single-line comments. Its designed to comment only one line in the Java code.

Comments on a single line are preceded by two forward slashes (//).

Java ignores any text between // and the end of the line (The compiler won’t execute it).

The following example uses a single-line comment before a line of code:

Example:

public class Main { public static void main(String[] args) { // This line is used as a comment here System.out.println("We are learning about the comments in Java"); } }

Example: 

public class Main{ public static void main(String[] args) {System.out.println(30+10);// Here + is used as an operator here and this commented line shows how the answer is calculated such as 30+10=40 } }

 

In this example, a single comment is added to the end of a line of code:

Example:

public class Main { public static void main(String[] args) { System.out.println("We are using a commented line at the end of line of code "); // This is a comment line } }

Example: 

public class Main{ public static void main(String[] args) {System.out.println(3*10); // The result will be 3 x 10= 30} }

Java Multi-line Comments

Multiple lines of code can be commented using the multi-line comment.

Commenting multiple lines of Java code at once is simpler (since single-line comments will be difficult to use in such a situation). It can be used to explain a complex code snippet or to comment multiple lines of code at once.

The multi-line comment begins with /* and ends with */.

Java will ignore a string between /* and */.

To explain the code, we use a multi-line comment (a comment block):

Example:

public class Main { public static void main(String[] args) { /* The code below will print the words All the lines above are commented uses multi line comment to the screen, and these two sentences will remain as a comment */ System.out.println("All the lines above are commented uses multi line comment "); } }

Example: 

public class Main { public static void main(String[] args) { /* We have created the multiline comments which will not affect the code down below and can contain all the details regarding the code in between them */ System.out.println("We are learning about multi line comments in this session ");} }

Is it better to use a single-line comment or multiple lines?

The choice is yours. Normally, we use // for shorter comments and /* */ for longer ones. 

Why do we comment in code?

  • Adding comments to a Java program helps it be more readable.

  • This makes it easier to maintain the code and to find errors.

  • You can use comments to explain or provide information about variables, methods, classes, or any statement.

  • While testing alternative Java code, it can also prevent execution of program code.

    Java Comments

    You now have a better understanding of how to use comments in Java programs

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *