0% found this document useful (0 votes)
26 views5 pages

Java 21: Comments and Variables Guide

The document outlines a Computer Programming 2 course, including learning objectives about comments and variables in Java. It discusses single-line, multi-line, and documentation comments, and different variable types like String, int, double, and Boolean. The activities at the end ask students to identify comment syntax, declare sample variables, and apply comments to a previous coding challenge.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Java 21: Comments and Variables Guide

The document outlines a Computer Programming 2 course, including learning objectives about comments and variables in Java. It discusses single-line, multi-line, and documentation comments, and different variable types like String, int, double, and Boolean. The activities at the end ask students to identify comment syntax, declare sample variables, and apply comments to a previous coding challenge.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Polytechnic Institute of Tabaco

100 Panal, Tabaco City


A.Y. 2021-2022

Course Code: IT3


Course Title: Computer Programming 2
Instructor: Tom Justine Militante
Course Schedule: Saturday, 1:00 - 3:00 pm
Module No. : 2

Learning Objectives:

At the end of this lesson, the students will be able to:


1. Learn how comments work
2. Determine the different types of comments
3. Define a single-line comment
4. Construct Multi-line Comments
5. Apply Documentation Comments in java
6. Be introduced to the concept of variables
7. Be familiarized with the different data types in java
8. Declare variables of various data types

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:

// this is a single-line comment


x = 5; // a single-line comment after code
Key Note:

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.

/* This is a single-line comment:

// 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 */

/** This is also 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:

/**********************

This is the start of a method

***********************/

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.

Variables have types. Some examples:


-int: for integers (whole numbers) such as 123 and -456
-double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or
scientific notations, such as 3.1416, -55.66.
-String: for texts such as "Hello" or "Good Morning!". Text strings are enclosed within double quotes.

You can declare a variable of a type and assign it a value.

Example:

String name = "David";


This creates a variable called name of type String, and assigns it the value "David".

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".

Examples of variable declarations:

class MyClass {
public static void main(String[ ] args) {
String name ="David";
int age = 42;
double score =15.9;
char group = 'Z';
}
}

char stands for character and holds a single character.

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.

1. Single-line comments are created using:

a) // characters at the end of the line


b) ** characters at the beginning of the line
c) // characters at the beginning of the line
d) */ characters at the beginning of the line
2. Make this text a multi-line comment.

_____ some

* comment text

_____

3. Which variable type would you use for a city name?

a) double
b) String
c) int

4. Complete the code to have a valid Java program.

class Apples {

public static void main(String[ ]args) {

_______ name = "John";

_______ age = 24;

_______ height = 189.87;

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!

Common questions

Powered by AI

In Java, variables are declared by specifying a data type followed by a variable name. For example, 'int age = 42;' declares an integer variable 'age'. Associating variables with specific types ensures type safety, which means that the variable is constrained to store values only compatible with its declared type, preventing runtime errors and aiding in program reliability .

When choosing a data type in Java, it is crucial to consider the size and nature of the data. For numeric data, choose between integer types (int, byte) for whole numbers, or floating-point types (double, float) for decimal values. Use char for single characters and String for text sequences. Boolean types are ideal for true/false conditions. This choice impacts memory usage and the operations that can be performed on the data .

In Java, you can declare and initialize multiple variables of the same type in a single line using a comma-separated list. For example, 'int a = 42, b = 11;' declares two integer variables, a and b, with respective initial values. This helps streamline code, particularly when variables share context or initialization logic .

The Javadoc tool reads documentation comments in the format of '/** ... */' from the source code and generates documentation in HTML format. This tool organizes code documentation for easy reference, especially useful for sharing with developers and users. It ignores redundant asterisks within the comments, focusing only on the content for generating documentation .

Comments are recommended primarily because they enhance the clarity and understanding of the code, both for the original programmer revisiting their code in the future and for other developers who might work with it later. Comments explain the purpose and functionality of the code, minimizing possible confusion and errors during future maintenance or upgrades .

The appropriate variable type for storing a person's height in a Java program would be a double. This is because a person's height is often represented as a decimal value, requiring a floating-point type. For example, 'double height = 189.87;' effectively stores the height with sufficient precision .

Single-line comments in Java are denoted by two forward slashes (//) and extend until the end of the line. They are used for brief annotations or explanations inline with code . Multi-line comments span multiple lines, starting with a '/*' and ending with a '*/', used for more extensive descriptions but cannot be nested . Documentation comments begin with a '/**' and conclude with a '*/', and are utilized to generate external documentation via the Javadoc tool .

Java does not support nested multi-line comments because the compiler does not distinguish between the start and end of such nested sequences, leading to logical errors or unexpected termination. However, single-line comments can appear within multi-line comments without issue because they are treated merely as lines of text within the multi-line comment, preserving the integrity of the block .

The Java compiler ignores all characters within comments—whether single-line, multi-line, or documentation comments. This allows developers to include explanatory notes, temporary deactivations of code lines, and structured documentation without affecting the program's execution. Thus, comments serve as useful in-code documentation, aiding understanding and maintenance without impacting performance .

The Boolean data type in Java is significant because it represents only two possible values: true or false. This binary logic is fundamental for control structures like loops and conditionals. For instance, 'boolean isLoggedIn = true;' might be used to track if a user is authenticated within an application .

You might also like