Difference between revisions of "Comments"

From CompSciWiki
Jump to: navigation, search
(Added a summary section)
m ("do" to "to")
 
(One intermediate revision by one other user not shown)
Line 17: Line 17:
 
}}
 
}}
  
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.
+
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===
 
===Block Comments===
Line 35: Line 35:
 
}}
 
}}
  
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 seperation to the comment.
+
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.
  
  
Line 75: Line 75:
 
==Summary==
 
==Summary==
  
The two types of comments, block and inline, help to document you 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>.
+
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.