Summary: In this tutorial, you will learn about comments in Java with the help of examples.

In Java, a comment is a piece of text in a program that is not executed as part of the program’s instructions.

Comments are used to provide explanations or notes about the code, or to temporarily disable certain lines of code.

There are two types of comments in Java:

  1. Single-line comment
  2. Multi-line comment

Single Line Comment

Single-line comments start with two forward slashes (//) and continue until the end of the line. For example:

// This is a single-line comment

Multi Line Comment

Multi-line comments start with a forward slash and an asterisk (/) and end with an asterisk and a forward slash (/). Everything between these symbols is considered a comment. For example:

This is a
multi-line comment

Here’s an example of how comments can be used in a Java program:

public class Main {
  public static void main(String[] args) {
    // Declare a variable
    int x;
    Set the value of x to 10.
    This value will be used in the following calculations.
    x = 10;
    // Perform some calculations
    int y = x * 2;
    int z = x + y;
    // Print the result to the console
    System.out.println("The result is: " + z);

Leave a Reply