Comments are basically lines that give us a brief description of the code written without getting interpreted by the compiler.
Let’s create a Comment in Java
A comment begins with 2 forward slashes. Let’s see an example to understand it better.
// This is a comment
Types of Comments
Comments can basically be submerged into the following types:
1. Single Line Comments
2. Multi Line Comments
Single Line Comments
The Single Line Comment is the easiest type of comment. It starts with 2 forward slashes and is further followed by an explanation.
It is used for some quick explanation of the code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program prints Hi | |
System.out.println("Hi"); |
OUTPUT:
Hi
Multi Line Comments
A comment spread over multiple lines is called a multi line comment. Let’s go over the following example to understand multi line comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This program prints the sum of 2 variables | |
here a is 1 and b is 2 | |
so c will be 3 | |
*/ | |
int a = 1; | |
int b = 2; | |
int c = a + b; | |
System.out.println(c); |
OUTPUT:
3