This file contains hidden or 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 file contains hidden or 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
description: In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way.
---
import { Heading } from '@/components/Heading'
import Link from 'next/link'
import { TipInfo } from '@/components/Tip'
In computer programming, comments are a portion of the program that are completely ignored by Java compilers. They are mainly used to help programmers to understand the code. For example,
```java
// declare and initialize two variables
int a =1;
int b = 3;
// print the output
System.out.println("This is output");
```
Here, we have used the following comments,
- declare and initialize two variables
- print the output
## Types of Comments in Java
In Java, there are two types of comments:
- single-line comment
- multi-line comment
### Single-line Comment
A single-line comment starts and ends in the same line. To write a single-line comment, we can use the `//` symbol. For example,
```java
// "Hello, World!" program example
class Main {
public static void main(String[] args) {
{
// prints "Hello, World!"
System.out.println("Hello, World!");
}
}
```
Output:
```text
Hello, World!
```
Here, we have used two single-line comments:
- `"Hello, World!" program example`
- `prints "Hello World!"`
The Java compiler ignores everything from `//` to the end of line. Hence, it is also known as **End of Line** comment.
### Multi-line Comment
When we want to write comments in multiple lines, we can use the multi-line comment. To write multi-line comments, we can use the `/*....*/` symbol. For example,
```java
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
{
System.out.println("Hello, World!");
}
}
```
Output:
```text
Hello, World!
```
Here, we have used the multi-line comment:
```java
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
```
This type of comment is also known as **Traditional Comment**. In this type of comment, the Java compiler ignores everything from `/*` to `*/`.
## Use Comments the Right Way
One thing you should always consider that comments shouldn't be the substitute for a way to explain poorly written code in English. You should always write well structured and self explaining code. And, then use comments.
Some believe that code should be self-describing and comments should be rarely used. However, in my personal opinion, there is nothing wrong with using comments. We can use comments to explain complex algorithms, regex or scenarios where we have to choose one technique among different technique to solve problems.
<TipInfo>
**Note:** In most cases, always use comments to explain **'why'** rather than **'how'** and you are good to go.
</TipInfo>
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Java comments docs #12
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Add Java comments docs #12
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing