Professional Documents
Culture Documents
Java Comments
To comprehend any programming language, there are several kinds of comments
which are used. Comments are notes written to a code for documentation purposes.
Those texts are not part of the program and do not affect the flow of the program.
Java supports three types of comments: C++‐style single line comments, C ‐style
multi‐line comments and special javadoc comments.
1. C++ ‐ style Comment
C++ Style comments starts with //. All the text after // are treated as comments.
We can write only a single line comment use these slashes.
Example:
// This is a single‐line comment in a java program.
2. C‐style Comment
C‐style comments or also called multi‐line comments starts with a /* and
ends with a */. All texts in between the two delimiters are treated as
comments. Unlike C++ style comments, it can span multiple lines.
Example:
/* This is a multiple line comment in a java program */
Example of identifiers:
Hello, main, System, out, public
Coding Guidelines:
1. For names of classes, capitalize the first letter of the class name.
For names of methods and variables, the first letter of the word
should start with a small letter.
For example:
ThisIsAnExampleOfClassName
thisIsAnExampleOfMethodName
2. In case of multi‐word identifiers, use capital letters to indicate the
start of the word except the first word.
For example:
charArray, fileNumber, ClassName.
Example:
2 //The decimal value 2
077 //The leading 0 indicates an octal value
0xBACC //The leading 0x indicates a hexadecimal value
Integral types have int as default data type. You can define its
long value by appending the letter l or L.
Range
Integer Length Name or Type
Named Constants
A named constant is a memory location whose content is not allowed to
change during program execution. To allocate memory, we use Java’s
declaration statement.
Syntax:
static final dataType IDENTIFIER = value;
In Java, static and final are reserved words. The reserved word final
specifies that the value stored in the identifier is fixed and cannot be changed.
The reserved word static may or may not appear when a named constant is
declared.
Coding Guidelines:
Notice that the identifier for a named constant is in upper case
letters. This is because Java programmers typically use upper case
letters to name a named constant. If the name of the named constant
is more than one word, called a run‐together‐word, then the words
are separated by an underscore.
Example:
final int RATE = 800;
final double CENTIMETERS_PER_INCH = 2.54;
Variables
A variable is an item of data used to store state of objects. It is a
memory location whose content may change during program
execution. It has a data type and a name. The data type indicates the
type of value that the variable can hold. The variable name must
follow rules for identifiers.
Syntax for declaration:
dataType identifier1, identifier2, … , identifierN;
Example:
double amountDue;
int counter;
char ch;
Primitive variables are variables with primitive data types.
They store data in the actual memory location of where the variable
is.
Reference variables are variables that store the address in the
memory location. It points to another memory location of where the
actual data is. When you declare a variable of a certain class, you are
actually declaring a reference variable to the object with that certain
class.
Example:
int num = 10;
String name = “Christi”;
20%2*3+6/2+155‐11;
((20%2)*3)+(6/2)+155‐10;
Test Yourself Getting the average of three numbers
Declaring and printing variables
Create a program that outputs the average of
Given the table below, declare the following three numbers. Let the values of the three
variables with the corresponding data types and numbers be, 10, 20 and 45. The expected
initialization values. Output to the screen the screen output is,
variable names together with the values.
number 1 = 10
Variable name Data Type Initial value number 2 = 20
number integer 10 number 3 = 45
letter character a
Average is = 25
result boolean true
str String hello
Output greatest value
The following should be the expected screen Given three numbers, write a program that
output, outputs the number with the greatest value
Number = 10
among the three. Use the conditional ?:
letter = a
result = true
operator that we have studied so far
str = hello (HINT: You will need to use two sets of ?: to
solve this). For example, given the numbers 10,
Operator precedence 23 and 5, your program should output,
Given the following expressions, re‐write them
by writing some parenthesis based on the number 1 = 10
sequence on how they will be evaluated. number 2 = 23
number 3 = 5
1. a / b ^ c ^ d – e + f – g * h + i
2. 3 * 10 *2 / 15 – 2 + 4 ^ 2 ^ 2
The highest number is = 23
3. r ^ s * t / u – v + w ^ x – y++