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

How to add different comments in Java code?

王林
Release: 2023-09-04 16:21:06
forward
1465 people have browsed it

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");
       }
    }
    Copy after login

    Output

    Welcome to Tutorials Point
    Copy after login
    Copy after login
    Copy after login

    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");
       }
    }
    Copy after login

    Output

    Welcome to Tutorials Point
    Copy after login
    Copy after login
    Copy after login

    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");
       }
    }
    Copy after login

    Output

    Welcome to Tutorials Point
    Copy after login
    Copy after login
    Copy after login

    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!

    Related labels:
    source:tutorialspoint.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template