Home >Java >javaTutorial >How to add different comments in Java code?

How to add different comments in Java code?

王林
王林forward
2023-09-04 16:21:061457browse

How to add different comments in Java code?

#Java comments are statements that are not executed by the compiler and interpreter. Comments can be used to provide information about variables, methods, classes, or any statement. It can also be used to hide program code at a specific time.

Types of Java annotations

Annotations in Java are divided into three types.

  • Single-line comments
  • Multi-line comments
  • Documentation comments
  • Single-line comments

    Single-line comments begin with // Start and end at the end of the line.

    Example 1

    public class SingleLineComment {
       public static void main(String[] args) {
          // To print output in the console
          System.out.println("Welcome to Tutorials Point");
       }
    }

    Output

    Welcome to Tutorials Point

    Multi-line comments

    Multi-line comments end with /*starting with*/ and span multiple lines.

    Example 2

    public class MultiLineComment {
       public static void main(String[] args) {
          /* To print the output as Welcome to Tutorials Point
             in the console.
          */
          System.out.println("Welcome to Tutorials Point");
       }
    }

    Output

    Welcome to Tutorials Point

    Documentation comments

    Documentation style comments end with /**&*/ and span multiple lines. Documentation comments must immediately precede a class, interface, method, or field definition.

    Example 3

    /**
       This is a documentation comment.
       This class is written to show the use of documentation comment in Java.
       This program displays the text "Welcome to Tutorials Point" in the console.
    */
    public class DocumentationComment {
       public static void main(String args[]) {
          System.out.println("Welcome to Tutorials Point");
       }
    }

    Output

    Welcome to Tutorials Point

    The above is the detailed content of How to add different comments in Java code?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete