Java Comments

Published by StudyMuch on

Java Comments studymuch

Java Comments

In this article you will read about the Java Comments, like what is Java comments, uses of comments in Java and also read the types of Java comments with programming examples.

What is Java Comment?

Comments in Java are the statements that are not executed by the compiler and interpreter. It can be used to provide information or execution about the variable, class and method or any statement to the programmers.

Uses of Comments

In any programming languages comments are use to explain the code and make it more readable. It can also used to prevent execution when testing alternative code.

Java Comment StudyMuch

Types of Comments in Java

Mainly there are three types of comments are used in Java programming languages, that are given below with programming examples.

  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

We defined below all the types Comments with programming examples.

Single Line Comment

The Single Line Comment in Java is used to comment only one line. In this when the programmers needs to write only one line comments and statements then they used single line comment method.

Syntax;

// This is Single Line Comment

Example of Single Line Comment;

public class comment {
  public static void main(String[] args) {
  // This is Single Line Comment
  System.out.println("StudyMuch");
    }
}

Above you can see the syntax and programming examples of single line comment, in example green line is the comment line.

Multi Line Comment

The Multi Line Comment in Java is used to comment multiple line of code statement in one time. If programmers need to write multiple lines of comments, then they can used multi line comment.

Syntax;

/* This is Multi Line Comment
    here we print
    StudyMuch website
 */

Example of Multi Line Comment;

public class comment {
  public static void main(String[] args) {
/* This is Multi Line Comment
   here we print
   StudyMuch website
 */
   System.out.println("StudyMuch");
    }
}

You can see above, syntax and programming example of Multi Line Comments, in this method you can write comment multiple lines of comments.

Documentation Comment

The Documentation Comment is used to create documentation API. To create documentation API.

Syntax;

/** This is Documentation
Comment to create
Documentation.
 */

Example of Documentation Comment;

/** The Calculator class provides
   Â Â Â Â Â Â Â methods to get addition
   and subtraction of given numbers.*/
  public class Calculator {
    /** The add() method returns
     Â Â Â Â Â Â Â Â Â Â Â  Sum of numbers.*/
  public static int add(int a, int b)
  { Â Â Â Â Â Â Â Â Â Â Â return a+b;
    }
  /** The sub() method returns
  Â Â Â Â Â Â Â Â Â Â Â  subtraction of numbers.*/
  public static int sub(int a, int
 b){ Â Â Â Â Â Â Â Â Â Â Â Â Â Â return
 a-b; Â Â Â }
}

You can see above programming examples of Documentation comments, the green line is documentation comments. You need to use Javadoc Too to compile this Documentation comments.


So, in this article you read about the comment of Java programming language with syntax and programming examples also. I hope you read well and learned something amazing. If you have any questions and doubts, please ask in the comment sections.

Read Also –


1 Comment

Ivy Lavender · March 12, 2023 at 4:06 am

To the studymuch.in webmaster, Your posts are always well-referenced and credible.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *