IT 3 - Computer Programming 2 Module 2
IT 3 - Computer Programming 2 Module 2
Learning Objectives:
Comments
The purpose of including comments in your code is to explain what the code is doing.
Java supports both single and multi-line comments. All characters that appear within a comment are
ignored by the Java compiler.
A single-line comment starts with two forward slashes and continues until it reaches the end of the
line.
For example:
Adding comments as you write code is a good practice, because they provide clarification and
understanding when you need to refer back to it, as well as for others who might need to read it.
Multi-Line Comments
Java also supports comments that span multiple lines. You start this type of comment with a forward
slash followed by an asterisk, and end it with an asterisk followed by a forward slash.
For example:
/* This is also a
comment spanning
multiple lines */
Note that Java does not support nested multi-line comments. However, you can nest single-line
comments within multi-line comments.
// a single-line comment
*/
Key Note:
Another name for a Multi-Line comment is a Block comment.
Documentation Comments
Documentation comments are special comments that have the appearance of multi-line
comments, with the difference being that they generate external documentation of your source code.
These begin with a forward slash followed by two asterisks, and end with an asterisk followed by a
forward slash.
For example:
/** This is a documentation comment */
Javadoc is a tool which comes with JDK and it is used for generating Java code documentation
in HTML format from Java source code which has required documentation in a predefined format.
When a documentation comment begins with more than two asterisks, Javadoc assumes that you
want to create a "box" around the comment in the source code. It simply ignores the extra asterisks.
For example:
/**********************
***********************/
Key Note:
This will retain just the text "This is the start of a method" for the documentation.
Variables
Variables store data for processing. A variable is given a name (or identifier), such as area, age,
height, and the like. The name uniquely identifies each variable, assigning a value to the variable and
retrieving the value stored.
Example:
Key Note:
It is important to note that a variable is associated with a type, and is only capable of storing
values of that particular type. For example, an intvariable can store integer values, such as 123; but it
cannot store real numbers, such as 12.34, or texts, such as "Hello".
class MyClass {
public static void main(String[ ] args) {
String name ="David";
int age = 42;
double score =15.9;
char group = 'Z';
}
}
Another type is the Boolean type, which has only two possible values: true and false. This data type
is used for simple flags that track true/false conditions.
For example:
boolean online = true;
Key Note:
You can use a comma-separated list to declare more than one variable of the specified type.
Example: int a = 42, b = 11;
Activities:
Answer the questions or comply with the instructions for each item below.
_____ some
* comment text
_____
a) double
b) String
c) int
class Apples {
Coding Challenge :
1. Create a simple java program, wherein 5 examples of your personal information are declared
as variables; given appropriate data types and identifiers.
2. Apply the different approaches towards commenting on your previous code, in the previous
module (Module 1: Coding challenge no.1).
Note: Use any text editor or IDE you prefer :) feel free to consult your instructor on how to execute the
activity. Thank you!