Comments in Java

Comments

What are Comments?

In computer programming, comments are part of the code that Java compilers entirely disregard.
They are mostly involved to help programmers in analyzing the code.

Here, in the page we will discuss more about the Comments that are used in java.

Comments in Java:

In any programming language, comments are the part of the code that are ignored by  the compiler. These comments are written in the program for the ease of the programmer. This helps a new programmer to understand the code of the program.

Syntax:

int a = 10;    // this is the example of a comment.
Comments in java

Types Of Comments:

There are mainly two types of comments that are used in the java programming. These comments are used to enhance the readability and the quality of the code. These are:

  1. Single-Line comments
  2. Multi-Line comments

Single-Line Comment:

A single-line comment starts and ends in the same line. To write a single-line comment, we can use the “// symbol

Syntax:

// I'm the syntax of single line comment.

Example:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        int num = 10;
        if(num==0){
            System.out.println("Zero");    // printing Zero
        }else if(num<0){    // checking if number is less than 0
            System.out.println("Negative");    
        }else{
            System.out.println("Positive");
        }
    }
}
Output:

Positive

Multi-Line Comment:

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.

Syntax:

/* I'm Multi line comment
You can write me in multiple lines */

Example:

Run
import java.util.*;

/* This program will check and print the number if 
it is positive, negative or Zero. */

public class Main{
    public static void main(String[] args){
        int num = 10;
        if(num==0){
            System.out.println("Zero");    
        }else if(num<0){    
            System.out.println("Negative");    
        }else{
            System.out.println("Positive");
        }
    }
}
Output:

Positive

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription