Programming is not that easy to understand sometimes. Let’s say you are a beginner, and you got a bunch of coding. Can you explain to them clearly if you are not familiar with each of the codes? Well, a comment in the java programming will help you to understand the tricky part in the java code. Therefore, we brought this article for you to describe Comments and types of comments.
If you are looking for a definition you can say, a comment is a part of programming that doesn’t take part in real coding but highlight the explanation of the particular coding to make it human-readable. A comment contains the detail of the reflected code. One of the best advantages of using comments is that using comments help finding the bugs. It also makes maintenance easier.
Comments and types of comments
Based on different uses of comments, it has three classes in Java coding. They are:
- Single line comment
- Multiple line comment
- Documentation comment.
Let’s discuss these comments and types of comments in a row. Hence we can understand their uses. Moreover, we can also learn how to use them in java programming.
Single Line Comment
A single line comment will allow you to demonstrate only one line. Just put “//” before the sentence line you want to show. You can also show another line below the sentence line you already showed. But you have to put “//” before every line. And thus, it is called single line comment type. Let’s look at the example; you will understand easily.
//here we have started our Java coding //you can mention the second line, but you must put //sign before your words.
package main; public class First { public static void main(String[] args){ // we write all of our codes after this line System.out.println("Welcome to infolinux.com"); //it is a println function } }
Multiple Line Comment
Sometimes, we can not mention a function using a line. In that case, we will need multiple line comment or documentation comment types. In this type of comment pattern, you do not need to put any sign before every line. Instead, you can put your required comments in the middle of “/*…..*/.” Check out the example. We have mentioned the comment type at the begging of the main program. Observe the sign front and back of the Java code.
/* In this programming, we will print “welcome to infolinux.com.” this comment here is an example of multiple line comment type Remember to write your comment in between the sign */
package main; public class First { public static void main(String[] args){ System.out.println("Welcome to infolinux.com"); } }
Documentation comment
Documentation comments are usually helpful for writing the code of a project/software package. In java programming, if hit enters in-between “/*…*/”
, the *
in front of each line will automatically appear. The program intro or the conclusion have this type of comment code. The following example will show you how to use documentation comment.
/** Your comment starts from here
* This page of Java coding will show you how to print
* We usually use println function
* Check it out here
*your comment end here*/
package main; public class First { public static void main(String[] args){ System.out.println("Welcome to infolinux.com"); } }
In Java Programming, We use these three types of comments. The single-line comment is prevalent for a single sentence. But when it comes for the multiple lines of sentences, Documentation comment is more popular than the numerous line comment because documentation comment is more convenient as the eclipse automatically generates how to use it. But remember, these comments don’t affect the output. All three of the examples has the same output.
