Java: Comments

Profile picture for user arilio666

Is your code clumsy? or do you need to avoid certain code steps in-between? In Java, we have a comment option just like any other programming language. We can comment on a code using three ways:

  • Single line comment (//)
  • Multi-line comment(/* */)
  • Documentation Comments(/** */)

1. Single line comment

A Single-line comment comments out a solo line in a code.

Example:

public class Testing
{
    public static void main(String[] args)
    {
        System.out.println("Hi there!");
        //System.out.println("Hello");
    }
}

Output: Hi there!

2. Multi-line Comment

  • A Multi-line comment comments out multiple lines of code.
  • Using /* before a code comments out the rest of the code you are writing down to the point of infinite. 
  • Use */ up to the point of code you want the comment to end and continue standard coding afterward.

Example:

public class Testing
{
    public static void main(String[] args)
    {
        /*
        int a = 50, b = 50 , sum;
        sum = a + b;
        System.out.println(sum);
        */
    }
}

3. Java Documentation Comments

  • These are the type of comments used up in a large program or for certain projects as it helps and acts as a documentation API. These APIs are the references for what really is happening in the code all Infos about classes, arguments used, and methods too.
  • The documentation comments are activated and used between /** and end with */.

Example:

/**  
* 
* Tags are used up here to represent something 
* Name of the author
* HTML tags can also b used   
* 
*/  

Javadocs Tags:

Here are some of the commonly used tags.

@author -> Only applied to the class, package, and overview level. The author tag is used to let know who made the significant changes.
@param -> A parameter only the method or the constructors accepts.
@deprecated -> Letting the user know that the method is no longer being used.
@return -> What actually the method returns.
@throws -> The exception thrown by the method.
@since -> Used to let know what the version was since used.

Why Comments?

  • In any programming, language comments prevent unwanted executions and make the code more readable.
  • In the comments, you can also give what the code is about as a description.
  • You can simply use comments and write pseudocode to actually take reference on what you are doing.
  • With Comments, you can notify and explain to any people with less knowledge of coding what you are doing in the code.
  • Comments are a significant stress relief for yourself to review the code after a long time.
Tags