COMMENTS IN JAVA


Comments In JAVA | Core Java Tutorial | Minigranth

Comments In Java : Introduction

  • Comments in java are the statements that are ignore by java compiler and hence are not executed.
  • They are used to describe the program, variable and method used to the other person who is reading the source code.
  • Also, generation of documentation of a class using documentation comment is one of the feature of comments in java.
  • Based on the usage, need and features, comments in java can be classified into three types each of which is explained in the later part of this chapter. Let's look at the types first:

This image describes the types of comments in java that can be used according to need and requirement at the time of development.
Comments In Java : Types

Comments In Java : The Discussion

  1. Single Line Comment
    • A single line comment is used to comment a single line.
    • Single line comment starts with //
    • Consider the example below with a sample program and its respective output.

    This image describes the sample program containing single line comment in java.
    Single Line Comment : Example


    This image describes output of the sample program containing single line comment in java.
    Single Line Comment : The comment line is ignored by the compiler.

  2. Multi- line Comment
    • Multi line comments are used to comment more than one line.
    • They start with /*  and ends with */
    • Consider the example below with a sample program and its respective output.

    This image describes the sample program containing multi line comment in java.
    Multi Line Comment : Example


    This image describes output of the sample program containing multi line comment in java.
    Multi Line Comment : The comment line is ignored by the compiler.

  3. Documentation Comments
    • Documentation comments are used to generate HTML file that provide documentation of your class.
    • Documentation comments starts with /** and ends with */
    • Consider the example below with a sample program and its respective output.

    This image describes the sample program containing documentation comment in java.
    Documentation Comment : Example


    This image describes output of the sample program containing documentation comment in java.
    Documentation Comment : The comment line is ignored by the compiler.

Note : To generate documentation we use command, javadoc  java_file.java