如何在 Java 代码中添加不同的注释?

javaobject oriented programmingprogramming更新于 2024/5/12 2:55:00

Java 注释是编译器和解释器不执行的语句。注释可用于提供有关变量、方法、类或任何语句的信息。它还可用于在特定时间内隐藏程序代码。

Java 注释的类型

Java 中有三种类型的注释。

  • 单行注释
  • 多行注释
  • 文档注释

单行注释

单行注释以 // 开头,以行末结束。

示例 1

public class SingleLineComment {
   public static void main(String[] args) {
      // 在控制台中打印输出
      System.out.println("Welcome to Tutorials Point");
   }
}

输出

Welcome to Tutorials Point

多行注释

多行注释以 /* 开头,以 */ 结尾,跨越多行。

示例 2

public class MultiLineComment {
   public static void main(String[] args) {
        /* 在控制台中将输出打印为 Welcome to Tutorials Point
         。
      */
      System.out.println("Welcome to Tutorials Point");
   }
}

输出

Welcome to Tutorials Point

文档注释

文档样式注释以 /** 开头并以 */ 结尾,跨越多行。文档注释必须紧接在类、接口、方法或字段定义之前。

示例 3

/**
   这是一个文档注释。
   此类用于展示 Java 中文档注释的使用。
   此程序在控制台中显示文本"Welcome to Tutorials Point"。
*/
public class DocumentationComment {
   public static void main(String args[]) {
      System.out.println("Welcome to Tutorials Point");
   }
}

输出

Welcome to Tutorials Point

相关文章