Comments in Java
Well, a comment is something that is going to be ignored. This means that whatever comment is there in the program, is going to be ignored. We usually write comments when we have to explain what some line of code does, or if we want some instruction in our program, but we don’t want that code to be executed, in this case also we can make use of comments. Comments are of two types –
- Single line comments
- Multi-line comments.
As the name suggests, single-line comments are the ones with which we can comment on only a single line, whereas, with a multi-line comment, we can comment on multiple lines.
In order to give a single-line comment, you have to make use of this symbol → //
So, whatever line you write after these two forward slashes, is going to be a single-line comment.
Let’s have a simple demonstration program to observe the single-line comments.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//This line is a single line comment, which is simply ignored
System.out.println(“This program explains comments”);
//The above line prints “This program explains comments” on the console.
}
}
So, if you try to run the above program, you will find that you get only “This program explains comments” as output and nothing else. This is because other lines written in the program are comments. You can simply understand that comments are ignored as if we are writing those lines of code in the program for our own understanding, and not for the implementation or execution. So, even if there is some line of code that is commented on, it is going to be ignored since it is a comment. You can refer to the below program for understanding this thing.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//This line is a single line comment, which is simply ignored
//System.out.println(“This program explains comments”);
//The above line prints “This program explains comments” on the console.
}
}
I have commented on the instruction we use to print something on the console. So, now nothing will print on the console, since the instruction now has become a comment. This way we can use comments to hide the instruction from being executed, but still, keep the line of code in the program.
Let’s now look over another type of comment, which is the multiline comments →
This is just another type of comment that lets us comment multiple lines at a time. Alternatively, you can declare each line as a single-line comment if you want, as we have done in the previous program. But we also have something called multi-line comments with us. We can use those too to comment on multiple lines at once, So, if you are wondering how can we give multi-line comments, let’s get into it. Even if you are not wondering about it, we have to get into it.
So, to create a multiline comment, you just have to write /**/.
So, whatever you write between /* and */ is a multi-line comment in java. (So basically here and becomes a comment, and it will be ignored when written inside a program)
Let’s have a look at a simple program to understand the multi-line comments →
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*
This line is a single line comment, which is simply ignored
System.out.println(“This program explains comments”);
The above line prints “This program explains comments” on the console.
33
*/
}
}
So, if you implement the above program, you can see that nothing happens because the line we want to execute is a comment now. You can make use of comments whenever you feel like keeping something in the program which you don’t want to execute, but you want the statement there. It may be some description or something else. You can use comments wherever you want in the program according to the need. In fact, you can also put some grocery lists into your program. Just kidding… it is not like we are writing anything in the comment just because it is ignored. There is no problem in writing a grocery list also, but it does not make sense there.