Java Comments

In a program, comments play an important role making the code readable to self & other developers

Its always a good practice to provide appropriate comments in your code. Its helps you and other developers to maintain a clean code and identify bugs in future. Comments are ignored by the compiler while compiling a code. Java supports 3 types of comments for the developers.

Java supports 3 types of comments for the developers.

* Single Line comment

Multi Line comment

Documentation comment

 

Lets now look at each comments in detail with an example.

SINGLE LINE COMMENT

//Line1  - Single line comment 

Eclipse IDE key-board shortcut (Keep the cursor in the exact line): 
: ctrl+/ 
: ⌘ + / 

MULTI-LINE COMMENT

/*Line1 
Line 2*/ 

Eclipse IDE key-board shortcut (Select the lines):
: Ctrl+shift+/
: ⌘ + / 

DOCUMENTATION COMMENT

/** 
*Line 1 
*@author : karthik
*@since : 2016
*/

This indicates documentation comment (java doc) . The JDK javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc, see the Java tool documentation.

(Note : Please look the notation carefully, this has 2 asterisk symbol at the beginning)

Eclipse IDE key-board shortcut (Place the cursor before the element for generating the doc) : 
: ctrl+alt+j ,
: ⌘ + Alt + J


Documentation comment have an in-built parameter specification which can be used by developer to input, so that java generator will be able to append these meta data for better clarity.

Parameter Description
@author karthik Describes an author.
@version Version 2.1 Display software version.
@since date/text Describes when this functionality was added
@see http://karthikk.com/index.php/myblog/java reference to other element of documentation. Can be a link or piece of code
@param UserName ,encryptedPassword Describes a method parameter.
@return boolean Describes the return value.
@exception classname description
@throws classname description
Exception that may be thrown from this method.
@deprecated description Describes an deprecated method.
{@inheritDoc} Copies the description from the overridden method.
{@link reference} Reference Link.
{@value #STATIC_FIELD} Value of a static field.
{@code literal} It is similar to <code>{@literal}</code>.
{@literal literal} The enclosed text is interpreted as not containing HTML markup or nested javadoc tags.

Print   Email