+ Reply to Thread
Results 1 to 6 of 6

Thread: Java:Reference - Proper Comments

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Java:Reference - Proper 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

    Prerequisites
    None.

    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
    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 
    */
    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:
    package helloworld;
    public class Hello {
    	private String name;
    	public hello(String aName) {
    		name = aName;
    	}
    
    	public String sayHello(){
    		Return "Hello, " + name + "!";
    	}
    }
    If this code were well commented it would look like this:

    Code:
    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 + "!";
    	}
    }
    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!

    Originally posted as Java Comments
    Last edited by John; 08-01-2010 at 10:01 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    I heard that there is a special comment type in Java that to include the comment into Java Documentation. Can you explain me about that a bit.

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    If you use the
    Code:
    /**
    
    */
    comments, the first line is used by the java docs as the general description, @return and @param are used by the java docs to include the return and parameters of methods.

  5. #4
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    Thanks a Bunch!
    And I think

    Code:
    /*
    ------
    ------
    ------
    */
    Can be used to multiple line comments and

    Code:
    /**
    -------
    -------
    -------
    */
    Can be used to Java Doc Comments.

  6. #5
    pankaj2007 is offline Newbie
    Join Date
    Feb 2007
    Posts
    7
    Rep Power
    0

    java editor

    can anyone tell the name of JAVA editor for developing different JAVA based applications.

  7. #6
    AfTriX is offline Programming God
    Join Date
    Jan 2007
    Location
    Chicago
    Posts
    586
    Rep Power
    0
    Quote Originally Posted by pankaj2007 View Post
    can anyone tell the name of JAVA editor for developing different JAVA based applications.
    Try to go through this thread, hope this would help you out with your problem.

    http://forum.codecall.net/java/2182-best-java-ide.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Proper jQuery Usage
    By Hunter100 in forum JavaScript and CSS
    Replies: 8
    Last Post: 03-29-2011, 11:02 AM
  2. Proper iteration.
    By FakeRobotGymnast in forum C and C++
    Replies: 1
    Last Post: 01-18-2009, 07:37 PM
  3. Proper For Loops?
    By thieflock in forum Java Help
    Replies: 2
    Last Post: 11-13-2007, 12:04 PM
  4. Java:Reference - Operators
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 12-09-2006, 08:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts