Skip to content

Commit f152551

Browse files
authored
Merge pull request #12 from javaistic/java-comments
Add Java comments docs
2 parents dfc3a2e + dc1a3b9 commit f152551

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export const documentationNav = {
1818
pages['operators'],
1919
pages['basic-input-output'],
2020
pages['expressions-statements-blocks'],
21+
pages['comments'],
2122
],
2223
}

src/pages/docs/comments.mdx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Java Comments
3+
description: In this tutorial, you will learn about Java comments, why we use them, and how to use comments in right way.
4+
---
5+
6+
import { Heading } from '@/components/Heading'
7+
import Link from 'next/link'
8+
import { TipInfo } from '@/components/Tip'
9+
10+
11+
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,
12+
13+
```java
14+
// declare and initialize two variables
15+
int a =1;
16+
int b = 3;
17+
18+
// print the output
19+
System.out.println("This is output");
20+
```
21+
Here, we have used the following comments,
22+
23+
- declare and initialize two variables
24+
- print the output
25+
26+
## Types of Comments in Java
27+
28+
In Java, there are two types of comments:
29+
30+
- single-line comment
31+
- multi-line comment
32+
33+
### Single-line Comment
34+
35+
A single-line comment starts and ends in the same line. To write a single-line comment, we can use the `//` symbol. For example,
36+
37+
```java
38+
// "Hello, World!" program example
39+
40+
class Main {
41+
public static void main(String[] args) {
42+
{
43+
// prints "Hello, World!"
44+
System.out.println("Hello, World!");
45+
}
46+
}
47+
```
48+
Output:
49+
50+
```text
51+
Hello, World!
52+
```
53+
Here, we have used two single-line comments:
54+
55+
- `"Hello, World!" program example`
56+
- `prints "Hello World!"`
57+
58+
The Java compiler ignores everything from `//` to the end of line. Hence, it is also known as **End of Line** comment.
59+
60+
### Multi-line Comment
61+
62+
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,
63+
64+
```java
65+
/* This is an example of multi-line comment.
66+
* The program prints "Hello, World!" to the standard output.
67+
*/
68+
69+
class HelloWorld {
70+
public static void main(String[] args) {
71+
{
72+
System.out.println("Hello, World!");
73+
}
74+
}
75+
```
76+
Output:
77+
78+
```text
79+
Hello, World!
80+
```
81+
Here, we have used the multi-line comment:
82+
83+
```java
84+
/* This is an example of multi-line comment.
85+
* The program prints "Hello, World!" to the standard output.
86+
*/
87+
```
88+
This type of comment is also known as **Traditional Comment**. In this type of comment, the Java compiler ignores everything from `/*` to `*/`.
89+
90+
## Use Comments the Right Way
91+
92+
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.
93+
94+
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.
95+
96+
<TipInfo>
97+
98+
**Note:** In most cases, always use comments to explain **'why'** rather than **'how'** and you are good to go.
99+
100+
</TipInfo>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy