Comments in Java – Types and Examples
Various programmers usually write, test, and verify a program code. Even on a small scale, a code must be understood by other viewers who examine it. Therefore, a small explanation or a note must be added to clear the air. Here is where the concept of Comments in Java originates.
The comments prevent the additional details given in the program from hindering the compiler. This article will lend you a helping hand in knowing about comments, their types, and their uses in Java.
What are Comments in Java?
The Java comments are nothing but lines that explain the code comprehensively. The compiler does not execute them; instead, they act as a bridge to make the user understand the code.
Uses of Comments in Java
1. Comment lines can be used to explain what a certain variable holds, the use of a method/function, class, and many more.
2. It can be used to avoid errors while testing other parts of the program.
3. Comments can also be used to mention other details.
4. This makes the program more readable.
Types of Java comments:
Java comments can be classified into three types:
1. Single line comment
2. Multi-line comment
3. Documentation comment
Single Line Comment in Java:
The single-line comment is used to comment only on a particular line. It is indicated using two forward slashes (//).
The compiler ignores any content following the // till the end of the particular line.
Syntax for Java Single-line comment:
// Text as comment (this line is not considered as a code)
Example code for Java Single-line comment:
public class FirstCode{ public static void main(String[] args) int num = 23; // num is a variable containing the value 23 System.out.println(num); // The value stored in num is printed } }
Output:
Multi-line comments in java:
As explained earlier, the single-line comments are helpful in avoiding the content present only in the particular line. However, this is less effective when the programmer has to elaborate the code in detail. Here is where the multi-line comments play a major role.
The comments start with /* and close with */. Therefore, any content between these symbols is ignored by the Java compiler.
Syntax for Java Multi-line comment:
/* Comment line 1 Continues Continues . . . Comment line n */
Example for Java Multi-line comments:
public class FirstCode{ public static void main(String[] args) int num = 23; /* comment line 1 comment line 2 comment line 3 */ System.out.println(num); } }
Output:
In the above example, the content between /* */ will not be executed. Multi-line comment symbol can also be used a single line comment as given below:
/* Comment line */
Java Documentation comment:
Documentation comments are used when the program’s size for the software or project is too large. In addition, it helps in generating a documentation page that can be referred to in the future.
The Java compiler ignores any content between the /** and */. It contains details about the class, methods, variables, and other useful information. The javadoc tool is used to create API documentation. The Documentation comments also contain various tags to represent various information.
Tags used in Java Documentation comment:
TAG | DESCRIPTION | SYNTAX |
@author | To denote the author of the class | @author name-text |
{@docRoot} | To denote the logical path to the root directory | {@docRoot} |
@code | To display the text in code format without interpreting it as a nested javadoc tag or HTML markup. | {@code text} |
@param | To include parameters and their description | @param parameter-name description |
@version | To add the “Version” subheading when it is used | @version version-text |
@since | To specify the “Since” heading when this text is generated | @since release |
@return | To specify that a value is returned | @return |
@throws | To denote that values are thrown | @throws class-name description |
@exception | To add a ‘Throws’ subheading to the documentation when it is generated. | @exception class-name description |
@deprecated | To a comment that the API should not be used | @deprecated deprecated text |
@see | To add a “See Also” heading that points to a reference | @see reference |
{@inheritDoc} | To inherit a comment from the nearest inheritable class. | Inheriting comment from immediate superclass |
{@link} | To insert an in-line link with | {@link package.class#member label} |
{@linkplain} | To add an identical {@link} that displays its name in a plain text format | {@linkplain package.class#member label} |
@serial | To indicate default serializable field | @serial field-description | include | exclude |
@serialData | To denote the documents written by writeObject() or WriteExternal() methods. | @serialField field-name field-type field-description |
{@value} | The {@value} displays the value of a constant | {@value package.class#field} |
Syntax for java documentation comment:
/** Comment begins * * tags to denote parameters * or methods and also headings * * Comment ends */
Sample program for Java documentation comment:
/** * <h1> Sum of Two Numbers </h1> * The SumNos program adds two numbers of integer value * the method returns the output * and it is presented on the screen. * * @author Programmer from FirstCode * @version 1.0 * @since 2022-06-01 */ Public class SumNos { /** * This method gives the sum of two integers. * @param num1 is the first parameter * @param num2 is the second parameter * @return is used to return the sum of num1 and num2. */ public int sumnos(int num1, int num2) { return (num1 + num2); } /** * The following is the main method that calls the SumNos method. @return nothing. public static void main(String args[]) { SumNos s = new SumNos(); int ans = s.sumnos(10, 23); System.out.println(“The sum of 10 and 23 is: ” +ans); } }
Output:
How can we execute Java comments?
In general, Java comments are not taken into account by the compiler while execution. Yet, the content can be encoded into ASCII before it is lexically transformed.
Sample program on Executable Java Comments:
public class FirstCode { public static void main(String[] args) { //\000d System.out.println(“Executing Java Comment”); } }
Output:
Explanation:
The above code prints the output because the compiler parses the Unicode \u000d as a new line before transforming it lexically. Therefore, the code is converted as:
public class FirstCode { public static void main(String[] args) { // System.out.println(“Executing Java Comment”); } }
As the comment is not applicable for the next line, it is printed.
Conclusion
The presence of comment lines makes the coding easily understandable by third parties. As we have seen all the types of comments and how they should be used in a program, you might be clear about it.
Thank you for viewing our article. Look out for further articles to learn more about the concepts in Java.