Java Comments Best Practices Example




Hey guys in this post, we will discuss different types of comments and their best practices in Java programming.

Overview


The Java comments are just statements in a program that the java compiler ignores it. The comments can be used to provide information or explanation about code or logic for future reference.

There are 3 types of Comments in Java:

  1. Single line comments
  2. Multi line comments
  3. Documentation comments

Single line comments


Take a look at the below example for single line comments

public class SingleLineComment {
	public static void main(String[] args) {
		// This is single line comment
		System.out.println("Hello java! This is single line comments example"); 
	}
}

As you can see, Single line comments starts with // (two slashes). Java compiler ignores anything that comes after // (two slashes)

Multi line comments


Take a look at the below example for multi line comments

public class MultiLineComments {
	public static void main(String[] args) {
		/*
		 this
		 is 
		 multi
		 line 
		 comments
		 */
		System.out.println("Hello java! This is multi line comments example"); 
	}
}

As you can see, Multi line comments starts with /* and ends with */. Java compiler ignores anything that comes after /* and before */

Documentation comments


Take a look at the below example for documentation comments

/**
 * @author Bushan
 * 
 * This is documentation comments
 */
public class DocumentationComments {
	public static void main(String[] args) {
		System.out.println("Hello java! This is documentation comments example"); 
	}
}

As you can see, Documentation comments starts with /** and ends with */. Java compiler ignores anything that comes after /** and before */



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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