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
Emmy
Emmy3w ago
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.
public class MyClass {
// This is a single-line comment

public void doSomething() {
/* This is a multi-line comment
...
possibly more text here
*/
}

/* multi-line comments can end in the same line they are starting */ private void otherMethod(){

}
}
public class MyClass {
// This is a single-line comment

public void doSomething() {
/* This is a multi-line comment
...
possibly more text here
*/
}

/* multi-line comments can end in the same line they are starting */ private void otherMethod(){

}
}
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.
/**
* This is the description of the class MyClass.
* This text will be exported when running the javadoc tool on this class.
*/
public class MyClass {
/**
* This is the method doSomething().
*/
public void doSomething();
}
/**
* This is the description of the class MyClass.
* This text will be exported when running the javadoc tool on this class.
*/
public class MyClass {
/**
* This is the method doSomething().
*/
public void doSomething();
}
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.
/**
* Obtains the length of a given {@link java.lang.String}.
* @param someString the String to obtain the length from
* @return the length of the String
*/
public int findStringLength(String someString){
return someString.length();
}
/**
* Obtains the length of a given {@link java.lang.String}.
* @param someString the String to obtain the length from
* @return the length of the String
*/
public int findStringLength(String someString){
return someString.length();
}
Emmy
Emmy3w ago
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 //).
/// Obtains the length of a given [java.lang.String].
///
/// @param someString the String to obtain the length from
/// @return the length of the String
public int findStringLength(String someString){
return someString.length();
}
/// Obtains the length of a given [java.lang.String].
///
/// @param someString the String to obtain the length from
/// @return the length of the String
public int findStringLength(String someString){
return someString.length();
}
Using markdown comments allows using Markdown syntax such as codeblocks, or italic blocks:
/// Obtains the _length_ of a given [java.lang.String].
///
/// This method can be called using
/// `int length = findStringLength("Hello World");`
///
/// @param someString the String to obtain the length from
/// @return the length of the String
public int findStringLength(String someString){
return someString.length();
}
/// Obtains the _length_ of a given [java.lang.String].
///
/// This method can be called using
/// `int length = findStringLength("Hello World");`
///
/// @param someString the String to obtain the length from
/// @return the length of the String
public int findStringLength(String someString){
return someString.length();
}
📖 Sample answer from dan1st
Emmy
Emmy3w ago
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.
Emmy
Emmy3w ago
thank you!
Submission from rosie_jen
Emmy
Emmy3w ago
There are two kinds of Java comments - per line and block comments. Line comments go like this
// Example of comment
// Example of comment
, while block comments go like this:
/* Example of a block comment,
I suppose */
/* Example of a block comment,
I suppose */
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
Emmy
Emmy3w ago
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
Emmy
Emmy3w ago
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 */
Emmy
Emmy3w ago
/* this is multi Line comment It can be in multiple lines Like this */
Submission from sinofforget
Emmy
Emmy3w ago
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.
public void doThings() {
// This method does things and other stuff
int result = getRandomNumber(); // This might be negative
}
public void doThings() {
// This method does things and other stuff
int result = getRandomNumber(); // This might be negative
}
/*...*/: 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.
private final int[] validValues = { 1, 4, 22, /* sometimes this is valid because reasons **/ 91 };

public void doThings() {
/*
We have to be careful when doing things. Sometimes the things become
broken, and then we get into a bad state. Check the current state as
we go and rollback if it goes sideways. Otherwise, commit the things
to make them permanent, then move on with our lives.
*/
}
private final int[] validValues = { 1, 4, 22, /* sometimes this is valid because reasons **/ 91 };

public void doThings() {
/*
We have to be careful when doing things. Sometimes the things become
broken, and then we get into a bad state. Check the current state as
we go and rollback if it goes sideways. Otherwise, commit the things
to make them permanent, then move on with our lives.
*/
}
/**...*/: 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.
/**
* This class handles my stuff.
*/
public class MyStuff {
/**
* Does the stuff.
*
* @param level The level to do stuff at. Must be a positive integer less than 1000.
* @return What stuff was done.
*/
public String doStuff(int level) {
return "done";
}
}
/**
* This class handles my stuff.
*/
public class MyStuff {
/**
* Does the stuff.
*
* @param level The level to do stuff at. Must be a positive integer less than 1000.
* @return What stuff was done.
*/
public String doStuff(int level) {
return "done";
}
}
Emmy
Emmy3w ago
///: 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.
/// This class handles my stuff.
public class MyStuff {
/// Does the stuff.
///
/// @param level The level to do stuff at. Must be a positive integer less than 1000.
/// @return What stuff was done.
public String doStuff(int level) {
return "done";
}
}
/// This class handles my stuff.
public class MyStuff {
/// Does the stuff.
///
/// @param level The level to do stuff at. Must be a positive integer less than 1000.
/// @return What stuff was done.
public String doStuff(int level) {
return "done";
}
}
⭐ Submission from dangerously_casual
Emmy
Emmy3w ago
// is inline comments, /* */ comments with mutiple lines, /** used to make documentation
Submission from javaobi

Did you find this page helpful?