Advertisements

Aatul Palandurkar

Writing Comments in Java

Posted on: June 17, 2011

Objective :

  • How to write comments in Java?
  • How to write single line comment in Java?
  • How to write multi line comment in Java?
  • How to write documentation comment in Java?
  • What are the types of comments in Java?

Writing Comments in Java :

  • Comments are the statements which are never executed (i.e. non-executable statements).
  • Comments are often used to add notes between source code so that it becomes easy to understand & explain the function or operation of the corresponding part of source code.
  • Java Compiler doesn’t read comments; comments are simply ignored during compilation.
  • There are 3 types of comments available in Java as follows;
  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

Single Line Comment :

This comment is used whenever we need to write anything in single line.

Syntax : 

//<write comment here>

Example :

//This is Single Line Comment.

Multi Line Comment :

This type of comments are used whenever we want to write detailed notes (i.e. more than one line or in multiple lines)related to source code.

Syntax :

/*

<write comment here>

*/

Example :  

/*
This
Is
The
Multi
Line
Comment
*/

Documentation Comment :

  • The documentation comment is used commonly to produce the javadoc for the respective program.
  • The javadoc is generally HTML, if used in project it is a set of multiple HTML files describing each java program in the corresponding project.
  • In the documentation comment we can add different notations such as author of the project or program, version, parameters required, information on results in return if any, etc.
  • To add these notations, we have ‘@’ operator.  We just need to write the required notation along with the ‘@’ operator.
  • Some javadoc tags are;
    1. @author – To describe the author of the project.
    2. @version – To describe the version of the project.
    3. @param – To explain the parameters required to perform respective operation.
    4. @return – To explain the return type of the project.

Syntax :

/**

*<write comment/description here>

*@author <write author name>

*@version <write version here>

*@param <write parameter here>

*@return <write return type here>

*/

Example :  

/**
* This is Documentation Comment.
* @author Atul Palandurkar
* @version 1.0.0
*/
Advertisements

11 Responses to "Writing Comments in Java"

yes this is very good . Thanks for it

Like

Given all comment is useful for the Java purpose.
so thanks a lot, Atul 🙂 Great Job dear friend….
JAVA at CETPA

Like

This explains what the output / result of the method is. In this case, it’s void.

Like

[…] As the figure describes, documentation part or writing comments into program is completely optional. If you want to know, how to write comments & documentation in java? Click here […]

Like

Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but instead of that, this is magnificent blog. A great read. I will definitely be back.

Like

thank you, this one is very useful to understand how to write documentation comments

Like

Yes Rahul, with the help of this tutorial people will be able to write comments easily.

Like

Hey this is a good one & very useful when writing projects, many don’t remember many time that for what code they have written & also helpful in easy understanding of projects later.

Like

Yes it helps a lot in understanding the project properly.

Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8,760 other followers

Tweets

%d bloggers like this: