Comments

Comments are ignored by the compiler but are useful to other programmers and to yourself when you read the code in future. There are three types of comments :

Regular comments which are ignored by the compiler:

// Single line comment which go to the end of the line

/* Block line comments, 
which can go to the closing */

Documentation comments which are parsed by the javadoc tool to generate documentation :

/**
This indicates a documentation comment. The compiler ignores this kind of comments
The javadoc tool uses documentation comments when preparing automatically generated documentation
*/
public class CommentsDemo {
    public static void main(String[] args) {
        //System.out.println("I will show when am commented out"); 
        //Run it, try removing the two slashes and run again

        /*
        This is a block comment, its very useful 
        when describing in detail what the code is doing
        Am going to use it below to describe what the program is doing
        */

        /*
        int[] numbers = {1,2,3,4,5,6,7,8,9,10};
        int sum = 0;
        for(int number : numbers){
            sum = sum + number;
        }
        System.out.println("Total " + sum);
        */

        //Uncomment the above block comment and Run again.

        //Observe how block comments are useful for commenting out
        //a huge portion of code. This is useful for debugging
    }
}
  

results matching ""

    No results matching ""