Difference between revisions of "Comments"

From CompSciWiki
Jump to: navigation, search
m (Changed the heading level for the Sample Program)
Line 21: Line 21:
  
  
===Example of a Java Program with Comments===
+
==Example of a Java Program with Comments==
  
 
[[Image:Code_block_p.png]]
 
[[Image:Code_block_p.png]]

Revision as of 14:34, 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.