Comments are a way of describing or explaining your code. In programming languages like Java, developers use comments to add instructions and code descriptions. Also, comments help you hide a specific (unnecessary) block of code from the compiler. These comments/instructions are readable to the users but ignored by the Java compiler. To use comments in Java, you can use different symbols like “//” and “/* … */”.
Java supports three types of comments: “single line”, “multi-line”, and “JavaDoc or Documentation”. The choice of comment type depends on your code requirements. In this Java guide, we will demonstrate the use of multiple-line comments in detail. But before proceeding with the multiple-line comments, let’s first understand what single-line comments are and how they work in Java.
How to Comment a Single Line in Java
The “//” symbol comments a single line in Java. The single-line comments are also referred to as inline comments as they impact/affect only one line of code.
So, if you want to comment on a single line in Java, simply begin that line with the “//” symbol, as demonstrated in the following code:
package exp;
//Creating a new Class
public class CommentsInJava {
//This is Java's main() method
public static void main(String[] args) {
//Initializing an array
String greetings[] = { "welcome", "to", "javabeat.net", "let's", "learn", "java" };
//Finding and printing size of the array
System.out.println("The Size of Given Array is ==> " + greetings.length);
//Iterating each element of the array using for loop
for (int i=0; i<greetings.length; i++)
{
System.out.println("Array Element at index " + i + " is :" + greetings[i]);
}
}
}
In the above code, we use single-line comments to provide relevant instructions to the user. Doing this allows any user to understand the code’s working easily. From the output, you can observe that the Java compiler ignores the comments and executes the actual code only:
This is how a single-line comment works in Java.
How to Comment Multiple Lines in Java
To comment multiple lines of code, enclose the code to be commented in the following structure/syntax “/* */”. The multi/multiple line comments are also known as block comments (as they comment/hide a block of code from the compiler). Here is a demonstration of the multiple-line comments in Java.
Suppose we want to concatenate the array elements to form a single string. But at the same time, we don’t want to remove the code of iterating the array(as it might be needed later). In this case, we can comment the unnecessary code as follows:
public class CommentsInJava {
public static void main(String[] args) {
String greetings[] = { "welcome", "to", "javabeat.net", "let's", "learn", "java" };
/*
System.out.println("The Size of Given Array is ==> " + greetings.length);
for (int i=0; i<greetings.length; i++)
{
System.out.println("Array Element at index " + i + " is :" + greetings[i]);
}
*/
String str = String.join(", ", greetings);
System.out.println("The Concatenated String ==> " + str);
}
}
The output verifies that the Java compiler didn’t read the commented code. Instead, it executes only those lines of codes that are not commented and retrieves the following output:
Important: You can also use the JavaDoc or documentation comments “/** … */” to comment your code. Generally, they are used before the class, method, or fields’ declaration to describe code elements. You can use the JavaDoc comments in your code for documentation comments/purposes.
What is the Shortcut Key to Comment Multiple Lines in Java
Other than the “/* */” symbol, you can also use the keyboard shortcut key “CTRL + /” to comment the selected lines of code. Doing this will add the “//” at the start of each comment line/statement. The “CTRL + /” shortcut key allows you to comment a single line or multiple lines. Here is a demonstration of the stated concept:
The output snippet shows that first, when we hit the run button all the code gets executed. In the second attempt, we comment a specific block of code and execute the code again. As a result, the commented block didn’t execute, which verifies the working of the “CTRL+ /” key.
How to Uncomment Your Code in Java
To uncomment the commented lines of code, select the commented code and press the “CTRL+ /” key. Here is a demonstration:
Note: This works if the code is commented using the “//” symbol. However, if the code is commented using “/* */”, then pressing the “CTRL+ /” key wouldn’t work.
Conclusion
To comment multiple lines in Java, wrap your code to be commented within the “/* */” symbol. The multiple-line comments are also known as multi-line or block comments. Also, Java allows you to implement multiple line comments with a keyboard shortcut key “CTRL + /”. For this purpose, select the lines of code to be commented using your mouse and then hit the “CTRL + /” key on your keyboard. This write-up has illustrated how to comment single or multiple lines of code in Java using relevant examples.