Java Comments

What is a comment? How do you use comments in Java? When should they be used? If you are wondering you should read on

The Idea

Probably the most important piece of code you can write is a comment. Comments not only make your code more clear to others, it will also help you to remember what you were thinking for future development.

The Solution

A comment is a piece of code that is ignored by the compiler that allows others to understand what is going on. In java there are two ways you can comment your code. You can use the traditional double forward slash like this
[code]//This is a comment[/code]
which comments everything to the left of the double forward slashes and ends with a new line. Or the other commonly used comment for commenting multiple is the forward slash asterisk like this:
[code]/**
using this
You can
Comment multiple
Lines
*/ [/code]
It is a common standard to comment every method in a class and include the parameters and return values. The java docs make specific use of this. Lets say we are giving a simple class like the one below:
[code lang="java"]package helloworld;
public class Hello {
private String name;
public hello(String aName) {
name = aName;
}

public String sayHello(){
Return "Hello, " + name + "!";
}
}
[/code]

If this code were well commented it would look like this:

[code lang="java"]package helloworld;
public class Hello {
private String name;
/**
* Constructs a Hello object that can greet a person.
* @param aName the name of the person who should be addressed.
*/

public hello(String aName) {
name = aName;
}
/**
* Greet with a "Hello" message.
* @return a message containing "Hello" and the name of the person.
*/
public String sayHello(){
Return "Hello, " + name + "!";
}
}
[/code]

The first line of a comment should be well formed to give the holistic idea of what that method does. In comments parameters are commented using the @param and return values are commented using @return. If you properly comment your documents, you can use Java’s javadoc utility to create a series of HTML files that document your classes and methods, but javadocs use your comments!

Related posts:

  1. Decimal to Roman Numeral Conversion [code lang="cpp"]#include #include using namespace std; string dec_to_numeral(int x) { int dec[13] = {1000, 900, 500, 400, 100, 90, 50,...
  2. Java Operators Arithmetic Operators [code][+] addition operator (also used for concatenation – see later tutorial) [-] subtraction operator [*] multiplication operator [/]...
  3. HTML with Swing Components One useful feature of Swing GUI’s many people overlook is the ability to use simple HTML tags within swing components....
  4. MouseListener Any program that actually does something is event driven. With out events, your program is entirely static and probably does...

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.