Difference between revisions of "Comments"

From CompSciWiki
Jump to: navigation, search
(Changed the introduction to be more user appropriate.)
Line 6: Line 6:
  
 
==Introduction==
 
==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 any problems when you go to run your program.
+
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.
 +
There are two types of comments: inline and block. 
  
===Example of a Java Program with Comments===
+
===Inline Comments===
Take a look at the example below. You will notice that some of the text does not look like Java code - they are normal English sentences.  Since Java will not understand these sentences, we use certain characters to denote that sections of text are '''comments'''. 
+
[[Image:Max_gpa_p.png]]
  
If you want to write a comment that is longer than one line, put <code>/*</code> at the beginning of the comment and <code>*/</code> at the end. In COMP 1010 you'll have to put a multi-line comment at the start of all assignment programs. Do this or they'll take off marks!
+
An inline comment allows comments do be added to a single line of code.  Everything after the <code>//</code> is ignored when the program is run.
  
You'll also have to (and actually want to) add comments to remind yourself, and anyone else who reads your program, about the various parts of your program. If the comment is only one line long, use <code>//</code>.  The text that appears after <code>//</code> is a comment.
+
===Block Comments===
 +
[[Image:Block_comment.png|Block Comment]]
  
<pre>
+
A block comment is for muliline 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 seperation to the comment.
/*******************************************************************************
+
  * 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
+
===Example of a Java Program with Comments===
  }
+
 
}
+
[[Image:Code_block_p.png]]
</pre>
+
  
To review, you should add comments to your programs (also called source code) to improve organization and to help someone else understand what your code does.
+
==WARNING==
 +
Comments make your code easier to read for everyone, including professors and their markers.
 +
Take a lesson from a former student: '''you will loose marks from your assignments if you do not document your code'''.
 
}}
 
}}

Revision as of 14:33, 22 November 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. There are two types of comments: inline and block.

Inline Comments

Max gpa p.png

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

Block Comments

Block Comment

A block comment is for muliline 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 seperation to the comment.


Example of a Java Program with Comments

Code block p.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 loose marks from your assignments if you do not document your code.

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