Java Lesson 3 - Comments

    Java comments are statements that are not compiled by the compiler. Therefore, they will not be executed when the Java program is running. The comments are used to provide information and explanation about the Java code that we write. Whenever someone else reads our code, comments make it easier for the person to understand our code. On some occasions, we can use comments to stop some of our code from being executed when we are testing our application. There are three types of comments,

  1. Single Line Comments
  2. Multi Line Comments
  3. Documentation Comments
1. Single Line Comments

    Single Line Comments start with two forward slashes (//) and any text between // and the end of the line will be ignored by Java compiler.
  • This is a Single Line Comment before a line of code:
        //This is a comment
        System.out.println("Hello Java");
  • This is a Single Line Comment at the end of a line of code
        System.out.println("Hello Java"); //This is a comment

2. Multi Line Comments

    Multi Line Comments start with /* and end with */. Any text between /* and */ will be ignored by Java.

  • This is a Multi Line Comment
        /*This is a
        Multi Line Comment*/
        System.out.println("Hello Java");

3. Documentation Comments

    The documentation comment is used to create documentation API. We will be able to generate a documentation for our classes in our Java application in HTML format like Java API documentation (String API Documentation). Documentation Comments start with /** and end with */. Any text between /** and */ will be used to describe our lines of codes and will be included in documentation HTML.
    /**This is my calculator class. It provides methods to do various
    mathamatical calculations such as addition, subtraction, etc*/
    public class Calcultor {
        /**The add() method returns addition of given numbers.*/
        public static add(int a, int b) {
            return a + b;
        }
        /**The sub() method returns subtraction of given numbers.*/
        public static sub(int a, int b) {
            return a - b;
        }
    }

Now we can generate our documentation using javadoc command.

    javadoc Calculator.java

This will generate HTML files for our documentation and we can open the index.html file and go through it.

We will discuss about Java Variables in next tutorial.

Comments

Popular posts from this blog