Difference between revisions of "Comments"

From CompSciWiki
Jump to: navigation, search
m
m ("do" to "to")
 
(31 intermediate revisions by 7 users not shown)
Line 1: Line 1:
We can add comments to our source code for better organization and to help someone else understand what our code does.
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Java Fundamentals]]
 +
|Previous=[[What is Programming?]]
 +
|Next=[[Output using System.out.]]
 +
|Body=
  
Take a look at the example below.
+
==Introduction==
  
<pre>
+
A comment is text you add to your source code to help make it easer to read, explain why you did something or some other documentation such as your professor asking for a header at the top of each file.  Comments can be written anywhere in your program and will not cause problems when you go to run your program.
/**
+
 
 +
==Types of Comments==
 +
 
 +
===Inline Comments===
 +
{{CodeBlock
 +
|Code=
 +
int max_gpa = 4.5;  // Declare a variable for the maximum gpa
 +
}}
 +
 
 +
An inline comment allows comments to be added to a single line of code.  Everything after the <code>//</code> is ignored when the program is run.
 +
 
 +
===Block Comments===
 +
{{CodeBlock
 +
|Code=
 +
/*******************************************************************************
 
  * PrintTemp
 
  * PrintTemp
 
  *
 
  *
Line 14: Line 32:
 
  * @author      Compsci Wiki
 
  * @author      Compsci Wiki
 
  * @version       
 
  * @version       
  */
+
  ***********************************************************************************/
 +
}}
 +
 
 +
A block comment is for multiline use.  You begin a block comment with <code>/*</code> and everything after until you reach a <code>*/</code> is the comment.  This is what you will normally see for file and method headers.  As you can see in the example below, extra <code>*</code>'s can be used to give better separation to the comment.
 +
 
 +
 
 +
 
 +
==Example of a Java Program with Comments==
 +
 
 +
{{CodeBlock
 +
|Code=
 +
/*******************************************************************************
 +
* PrintTemp
 +
*
 +
* This program shows you how to print the contents of a variable
 +
*
 +
* COMP 1010
 +
* Instructor:  Compsci Wiki
 +
* Assignment:  Assignment 0, question 0
 +
* @author      Compsci Wiki
 +
* @version     
 +
***********************************************************************************/
  
 
public class PrintTemp
 
public class PrintTemp
 
{
 
{
  public static void main(String args[])  
+
    public static void main(String args[])  
  {
+
    {
    int temp; //declare an variable of type integer
+
        int temp; //declare an variable of type integer
 +
        temp = 25; //store 25 in a variable
  
    temp = 25; //store 25 in a variable
+
        //print the temperature to the screen
 
+
        System.out.println("The temperature is currently " + temp + " degrees Celsius.");  
    System.out.println("The temperature is currently " + temp + " degrees Celsius."); //print the temperature to the screen
+
    }
  }
+
 
}
 
}
</pre>
+
}}
 +
 
 +
==[[Image:Warning_24.png]]WARNING==
 +
Comments make your code easier to read for everyone, including professors and their markers.
 +
Take a lesson from a former student: '''you will lose marks from your assignments if you do not document your code'''.
 +
 
  
We can use // before a single line to indicate that this line is a comment. If we want to include a comment that is longer than one line, we can put /* at the beginning of the comment and */ at the end.
+
==Summary==
  
When a program is compiled, the compiler ignores the comments. This means that comments do not effect the program. So, why do we use comments? Comments are only for you and any one else that reads your source code. Imagine someone gives you the source code to a long, complex program which does not contain any comments. If you wanted to understand the code you would have to spend a lot of time going through each line and figuring out what everything does. If this program had comments that explained the purpose of each class, method, and block of code it would make understanding the code a lot easier.
+
The two types of comments, block and inline, help to document your code. This documentation makes code easier to understand and maintain. In the next section you will start to learn how to make programs interactive by writing text to the monitor using <code>System.out</code>.
 +
}}

Latest revision as of 22:35, 7 December 2011

COMP 1010 Home > Java Fundamentals


Introduction

A comment is text you add to your source code to help make it easer to read, explain why you did something or some other documentation such as your professor asking for a header at the top of each file. Comments can be written anywhere in your program and will not cause problems when you go to run your program.

Types of Comments

Inline Comments

 int max_gpa = 4.5;  // Declare a variable for the maximum gpa 

An inline comment allows comments to be added to a single line of code. Everything after the // is ignored when the program is run.

Block Comments

 /*******************************************************************************
 * PrintTemp
 *
 * This program shows you how to print the contents of a variable
 *
 * COMP 1010
 * Instructor:   Compsci Wiki
 * Assignment:   Assignment 0, question 0
 * @author       Compsci Wiki
 * @version      
 ***********************************************************************************/ 

A block comment is for multiline use. You begin a block comment with /* and everything after until you reach a */ is the comment. This is what you will normally see for file and method headers. As you can see in the example below, extra *'s can be used to give better separation to the comment.


Example of a Java Program with Comments

 /*******************************************************************************
 * PrintTemp
 *
 * This program shows you how to print the contents of a variable
 *
 * COMP 1010
 * Instructor:   Compsci Wiki
 * Assignment:   Assignment 0, question 0
 * @author       Compsci Wiki
 * @version      
 ***********************************************************************************/

public class PrintTemp
{
    public static void main(String args[]) 
    {
        int temp; //declare an variable of type integer
        temp = 25; //store 25 in a variable

        //print the temperature to the screen
        System.out.println("The temperature is currently " + temp + " degrees Celsius."); 
    }
} 

Warning 24.pngWARNING

Comments make your code easier to read for everyone, including professors and their markers. Take a lesson from a former student: you will lose marks from your assignments if you do not document your code.


Summary

The two types of comments, block and inline, help to document your code. This documentation makes code easier to understand and maintain. In the next section you will start to learn how to make programs interactive by writing text to the monitor using System.out.

Previous Page: What is Programming? Next Page: Output using System.out.