How to comment code in Java? – Java Comments
A good program is one which is easy to understand by someone else. The developer can understand his code after struggling with it but it is not easy for someone else. In programming, language comments play an essential role in developing an understanding of code. A commented code is easy to read and understand. Similarly, in Java programming, comments are optional, however, a good practice. You may be interested in learning about identifiers in Java.
There are various software which can generate a developer’s manual from the comments the in code. Comments can be used to narrate or justify the reason for the existence of a particular line or piece of code. These are not executable and ignored by compilers and interpreters. Comments also help the developer during writing and testing code. The developer can comment the particular piece of code to avoid its execution and test the working of other code. You may be interested in watching video java tutorials.
There are two main forms of comments:
- Single-line comments. These comments are used for commenting a single line of code.
- Multi-line comments. These comments are used for commenting multiple lines of code.
Java Single-line Comments:
In Java programming language, we use two forward slashes consecutively to comment a single line of code. For example, in the given example below, the whole code will be executed except the lines starting with //.
1. public class Hello{ 2. public static void main(String args[]){ 3. //This line is a comment. 4. //The code below will print Hello World on screen. 5. System.out.println("Hello World"); 6. } 7. 8. }
In the above code, line no. 3 and 4 are comments. These will not be a part of the execution of code. Line no. 3 tells about itself that it is a comment, while line no. 4 tells about what the following code will do. It is best practice to write a comment about a code above it on a separate line, however, you can also give inline comments as shown below. You may be interested in learning Android Studio.
1. public class Hello{ 2. public static void main(String args[]){ 3. //This line is a comment. 4. 5. System.out.println("Hello World"); //The code below will print Hello World on screen. 6. } 7. 8. }
In the above code, look at line no. 5, you can see the inline comment. This will not be part of the execution of the program.
Java Multi-line Comments:
Now the second way to comment code in Java is multi-line comments. When a developer has to comment on a snippet or block of code, he uses multi-line comments. You can also call them as block comments. To commit multiple comments, the starting line should start with /* and ending line should end with */. To understand it, see the code below.
1. public class Hello{ 2. public static void main(String args[]){ 3. /* 4. This is multi-line comment, 5. The following code prints Hello World on screen. 6. */ 7. System.out.println("Hello World"); //The code below will print Hello World on screen. 8. /* 9. System.out.println("It will not be printed"); 10. System.out.println("Remove comments tags to print."); 11. */ 12. } 13. }
Now look at the line number 3, this is start of multi-line comment and line number 6 is end of multi-line comment. Similarly, line number 8 is start of multi-line comment and line number 11 is end of multi-line comment. The code at line number 9 and 10 will not be executed because it is in comments block. To execute this part, you will need to remove the comments.c