Hey guys in this post, we will discuss different types of comments and their best practices in Java programming.
Table of Contents
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:
- Single line comments
- Multi line comments
- 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 */
Really Excited to read your Article
Thanks & Regards
V.saibabu