Difference between revisions of "Comments"

From CompSciWiki
Jump to: navigation, search
m
(Doug's edits)
Line 1: Line 1:
{{1010Topic|Introduction=We can add comments to our source code for better organization and to help someone else understand what our code does.|Overview=Here you will learn the syntax needed to document your code.|Chapter_TOC=[[Your First Java Program]]}}
+
{{1010Topic|Introduction=We can add comments to our source code to improve organization and to help someone else understand what our code does.|Overview=Here you will learn the syntax needed to document your code through comments.|Chapter_TOC=[[Your First Java Program]]}}
Take a look at the example below. Notice the lines that are contained inside '''/*''' and '''*/'''. Also notice the lines that start with '''//'''. These lines are called comments.
+
Take a look at the example below. Notice the lines that are contained inside <code>/*</code> and <code>*/</code>. Also notice the lines that start with <code>//</code>. These lines are called comments.
  
 
<pre>
 
<pre>
Line 28: Line 28:
 
</pre>
 
</pre>
  
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.
+
We can use // to indicate that the text that follows 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.
  
When a program is compiled, the compiler ignores the comments. This means that comments do not affect 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.
+
When a program is compiled, the compiler ignores the comments. This means that comments do not affect the program. So, why do we use comments? Comments are only for you and any one else that reads your source code. To see the value of comments, 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. However, 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.

Revision as of 20:54, 21 March 2007

COMP 1010 Home > Your First Java Program


Introduction

We can add comments to our source code to improve organization and to help someone else understand what our code does.

   

{{{Body}}}

Take a look at the example below. Notice the lines that are contained inside /* and */. Also notice the lines that start with //. These lines are called 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

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

We can use // to indicate that the text that follows 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.

When a program is compiled, the compiler ignores the comments. This means that comments do not affect the program. So, why do we use comments? Comments are only for you and any one else that reads your source code. To see the value of comments, 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. However, 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.