Comments in Java

In Java there are 3 types of Comments:
  1. Single Line Comment
  2. Block Comment
  3. Java Documentation

1. Single Line Comment
    //single line comment
    Anything  after // (double forward slash) is ignore by compiler for that line of code
    
2. Block Comment
    /* 
     * Block
     * Comment 
     */
    It is also called Multi Line Comment. Any text inside /* */ is ignore by the compiler

3. Java Documentation
    //**
      * Java 
      * Documentation
      */
      Java documentation is ignore by the compiler but it will generate HTML file using javadoc tool

      Generate Java doc from command Line:
      javadoc FileName.java

Calculator.java
package com.java.comments;

/**
 * @author Anish Singh
 * @version 1.5
 * @since JDK 1.0
 * @see Object
 */

public class Calculator {
    public int first;
    public int second;

    /**
     * @param zero-parameter
     *            constructor
     */

    public Calculator() {
        first = 20;
        second = 10;
    }

    /**
     * @param two
     *            parameter constructor first and second
     */

    public Calculator(int first, int second) {
        this.first = first;
        this.second = second;
    }

    /**
     * @param a
     *            First number
     * @param b
     *            Second number
     * @return nothing
     */

    public void add(int a, int b) {
        int sum = a + b; // Find addition of a and b
        System.out.println("Addition of " + a + " and " + b + " is " + sum);
    }

    /**
     * @param x
     *            First number
     * @param y
     *            Second number
     * @return Subtraction of two numbers
     * @deprecated This method is no longer supported from now on
     */

    public static int min(int x, int y) {
        int dif = x - y; // Find difference of x and y
        return dif;
    }

    /**
     * @param num1
     *            First number
     * @param num2
     *            Second number
     * @return Subtraction of two numbers
     */

    public int sub(int num1, int num2) {
        int res = num1 - num2; // Find difference of num1 and num2
        return res;
    }

    /**
     * @param num1
     *            First number
     * @param num2
     *            Second number
     * @return multiplication of two numbers
     */

    public static int mul(int num1, int num2) {
        int res = num1 * num2; // Find multiplication of num1 and num2
        return res;
    }

    /**
     * @return Division of two numbers
     */

    public int div() {
        int res = first / second; // Find multiplication of num1 and num2
        return res;
    }

    /**
     * @return {@code void}
     * @param args
     *            String[]
     * @exception IOException
     *                {@code Exception} is root class of all the
     *                {@code Exception}
     * @see Exception
     * @throws IOException
     *             while taking input anything goes wrong It will throws
     *             IOException
     * @since JDK 1.0
     */

    public static void main(String[] args) throws Exception {
        Calculator cal = new Calculator();
        /*
         * Call add method with two parameter add method is non static so we to
         * call this method using Object of Calculator class
         */

        cal.add(cal.first, cal.second);

        /*
         * Call min method with two parameter min method is static so we to call
         * this method using class name Calculator This method is deprecated
         */

        System.out.println("Subtraction of " + cal.first + " and " + cal.second + " is: " + Calculator.min(cal.first, cal.second));

        /*
         * Call sub method with two parameter sub method is non-static so we to
         * call this method using object of class Calculator
         */

        cal = new Calculator(100, 25);
        System.out.println("Subtraction of " + cal.first + " and " + cal.second + " is: " + cal.sub(cal.first, cal.second));

        /*
         * Call mul method with two parameter mul method is static so we to call
         * this method using class name Calculator
         */

        System.out.println("Multiplication of " + cal.first + " and " + cal.second + " is: " + Calculator.mul(cal.first, cal.second));

        /*
         * Call div method with zero parameter div method is non-static so we to
         * call this method using object of class Calculator
         */

        System.out.println("Division of " + cal.first + " and " + cal.second + " is: " + cal.div());
    }
}

Output:
Addition of 20 and 10 is 30
Subtraction of 20 and 10 is: 10
Subtraction of 100 and 25 is: 75
Multiplication of 100 and 25 is: 2500
Division of 100 and 25 is: 4

No comments:

Post a Comment