Java Comments

    Java Comments

    The Java comments are the explanatory statements that do not get executed by an interpreter or a compiler . It allows you to mention the working of any statement or used to hide the program code.

    There are three types of comments that are supported by Java-

    • Single line comments
    • Multiple line comments
    • Documentation comments

    Single-line Java Comment

    If you want to comment the single line, you can use the single line Java comment using “ // ” at the start of the statement.

    Example

    public class Example1 {  
    
    public static void main(String[] args) {  
    
        int i=5;//Here, i is a variable  
    
        System.out.println(i);  
    
    }  
    
    }  

    Output

    5

    Multi-line Java Comment

    If you want to comment multiple lines, you can use multi-line Java comments. The statements can be commented between “ /*....*/ ”.

    Example

    public class Example2 {  
    
    public static void main(String[] args) {  
    
    /* Let's declare and 
    
     print variable in java. */  
    
        int i=5;  
    
        System.out.println(i);  
    
    }  
    
    }  

    Output

    5

    Documentation Comment

    You can use this comment when you are creating the documentation API. For this, you have to use the Javadoc tool.

    Syntax

    /** 
    
    documentation  
    
    comment 
    
    */  

    Example

    public class Calculator {  
    
    /** The add() method.*/  
    
    public static int add(int a, int b){return a+b;}  
    
    /** The sub() method */  
    
    public static int sub(int a, int b){return a-b;}  
    
    }  

    Above code can be compiled by the javac tool, and then create the documentation API using the Javadoc tool.

    javac Calculator.java
    
    javadoc Calculator.java

    This will create the HTML file for the calculator class in the directory.