Week 141 — What kinds of comments can be used in Java and how do they differ?
Question of the Week #141
What kinds of comments can be used in Java and how do they differ?
11 Replies
To create a comment that doesn't change the behavior the application, one can either use a single-line comment that starts with
//
(which automatically ends at the end of the line) or a multi-line comment starting with /*
and ending with */
. While multi-line comments can span over multiple lines, they can also end in the same line where they started by adding the closing */
in the same line.
If a multi-line comment starts with /**
instead of */
and is placed before certain Java elements (types, constructors, members (methods/fields), etc), it is considered to be a Javadoc comment. These comments can be read by the javadoc
tool to generate and export documentation.
Javadoc comments can make use of Javadoc tags to provide additional information. For example, it is possible to document method parameters using the @param
tag while the return type can be documented using @return
.
In addition to that, it is possible to use markdown for Javadoc comments starting in JDK 23. These markdown comments consist of single-line comments starting with
///
(instead of just //
).
Using markdown comments allows using Markdown syntax such as codeblocks, or italic blocks:
📖 Sample answer from dan1st
a comment is a non -executable, non-graphic statements in java. they start with a front-slash (/) and are for the user's understanding. these are ignored by the compiler.
there are 3 types of comments in java
1. a single-line comment - this comment is followed by two forward-slashes (//). it is used to display a comment in the editor window which is one line long.
2. multi-line comment - this comment is enclosed within /* and /. it is used to display a comment in the editor window which consists of multiple lines.
3. documentation comment - this comment is enclosed within// and /. it is used to display a comment in the editor window which consists of long paragraphs or several long, detailed sentences.
example code of single-line comment:
// this program is to show the usage of single line comments
class single_line_comment
{ //class created
public static void main ()
{ //method created
System.out.println ("Hello!");
} //main ends
} //class ends
every statement followed by // are only displayed on the editor window and not the terminal screen.
the output on the terminal screen is:
Hello!
example code of a multi-line comment:
/ this program is to show the
* sum of two numbers
* a and b
/
class multi_line_comment
{
public static void main ()
{
int a = 23;
int b = 67;
int sum = a+b;
/ here we have done dynamic initialisation
* of a, b and sum
/
System.out.println ("Sum of two numbers is = "+sum);
}
}
every statement enclosed within / and */ is a multi line comment. this code shows 2 multi-line comments which are seen in the code editor window.
the output of the code is:
Sum of two numbers is = 90
example code of a documentation comment:
class documentation_comment
{
/ this is a program to show the addition
* of two numbers a and b
* @param a as first number
* @param b as second number
* @return sum of a and b
*/
public static void main ()
{
float a = 89.5f;
float b = 26.9f;
float sum = a+b;
System.out.println ("The sum of two numbers is = "+sum);
}
}
the statements enclosed in / and */ form the documentation comment.
the output of the code is:
The sum of two numbers is = 116.4
thus, we have seen the purpose of the 3 types of comments in java.
thank you!
Submission from rosie_jen
There are two kinds of Java comments - per line and block comments.
Line comments go like this , while block comments go like this:
Line comments are helpful for short descriptions of classes, functions while block comments are more expected for more detailed documentation. Line comments can be anywhere in the line of a code, while block comments should start on their own line.
Submission from .velummortis
In Java, the three primary comment types are the single line comment (//) for brief in-line notes, the multi line comment (/* ... */) for longer descriptions or temporarily disabling code blocks, and the critically important Javadoc comment (/** ... */) which is used to formally generate comprehensive API documentation for classes, methods, and parameters, detailing their purpose, inputs, return values, and exceptions.
Submission from timres0412
1. single line comment
Can only write in one one line
'// ' starts with
Ex:
// this is single line comment
Int X = 10
2. Multi line comment
Can write comments in multiple lines
Starts with /* and ends with */
/* this is multi Line comment
It can be in multiple lines
Like this */
Submission from sinofforget
Java supports 4 kinds of comments:
1.
//
- Line comments
2. /*...*/
- Multi-line comments
3. /**...*/
- JavaDoc comments
4. ///
- Markdown JavaDoc comments
//
: Line comments can be placed on their own line or at the end of a line of code. Everything after the //
until the end of the line is considered a comment.
/*...*/
: Multi-line comments start with a /*
. Everything after that -- including newlines -- is a comment, until a closing */
is reached. Note that these comments do not need to span multiple lines. Because they have an explicit end, they can also be used to embed a comment within a line of code.
/**...*/
: JavaDoc comments are used to add documentation to a program element, which can then be processed by the JavaDoc tool to generate API documentation. The doc comments can embed HTML and JavaDoc tags (elements like @param
or {@link ...}
). Note that the *
starting each line of text is a formatting convention, and will not be present in the generated output.
///
: All of the above comment types have been present in Java since the 1.0 release. But since Java 23, Markdown (specifically, CommonMark Markdown) can be used for JavaDoc comments. Just like the traditional JavaDoc comments above, these are processed by the JavaDoc tool to generate API documentation. These allow the developer to use Markdown for documentation formatting and linking, rather than embedding HTML. JavaDoc tags can also still be used.
⭐ Submission from dangerously_casual
// is inline comments, /* */ comments with mutiple lines, /** used to make documentation
Submission from javaobi