Java Programming
Fundamentals
Elementary Programming
By Kiswick Chisamu
4. Identifiers
By Technical Instructor, Mr K Chisamu 2
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 3
Identifiers
• Identifiers are the names that
identify the elements such as
classes, methods, and variables
in a program.
By Technical Instructor, Mr K Chisamu 4
Identifier Naming Rules
• All identifiers must obey the
following rules:
By Technical Instructor, Mr K Chisamu 5
Identifier Naming Rules:
#1
• An identifier is a sequence of
characters that consists of letters,
digits, underscores (_), and
dollar signs ($).
By Technical Instructor, Mr K Chisamu 6
Identifier Naming Rules:
#2
• An identifier must start with a
letter, an underscore (_), or a
dollar sign ($). It cannot start with
a digit.
By Technical Instructor, Mr K Chisamu 7
Identifier Naming Rules:
#3
• An identifier cannot be a reserved
word.
By Technical Instructor, Mr K Chisamu 8
Identifier Naming Rules:
#4
• An identifier cannot be true,
false, or null.
By Technical Instructor, Mr K Chisamu 9
Identifier Naming Rules:
#5
• An identifier can be of any
length.
By Technical Instructor, Mr K Chisamu 10
Valid Identifiers
• For example,
1. $2,
2. ComputeArea,
3. area,
4. radius,
5. print
• are legal identifiers
By Technical Instructor, Mr K Chisamu 11
Valid Identifiers
• whereas
• 2A
• d+4
• are not because they do not follow
the rules.
• The Java compiler detects
illegal identifiers and reports
syntax errors.
By Technical Instructor, Mr K Chisamu 12
Descriptive Variable
Names
• Identifiers are for naming
variables, methods, classes, and
other items in a program.
• Descriptive identifiers make
programs easy to read.
By Technical Instructor, Mr K Chisamu 13
Java is Case Sensitive
• For example,
• numberOfStudents is better than
• numStuds,
• numOfStuds,
• numOfStudents.
• We use descriptive names for
complete programs in the text.
By Technical Instructor, Mr K Chisamu 14
The $ Character
• Do not name identifiers with the
$ character.
• By convention, the $ character
should be used only in
mechanically generated source
code.
By Technical Instructor, Mr K Chisamu 15
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 16
QUESTION 1 [12
Marks]
• Which of the following
identifiers are valid?
• Which are Java keywords?
• miles, , Test, , a++, --a,
4#r, $4, #44, apps, class,
public. Int, x, y, , radius.
By Technical Instructor, Mr K Chisamu 17
5. Varibales
By Technical Instructor, Mr K Chisamu 18
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 19
Variables
• Variables are used to represent
values that may be changed in the
program.
• Variables are used to store
values to be used later in a
program.
By Technical Instructor, Mr K Chisamu 20
Why The Name Variables?
• They are called variables because
their values can be changed.
By Technical Instructor, Mr K Chisamu 21
By Technical Instructor, Mr K Chisamu 22
Variables
• Variables are for representing
data of a certain type.
• To use a variable, you declare it
by telling the compiler its name as
well as what type of data it can
store.
By Technical Instructor, Mr K Chisamu 23
Variables
• The variable declaration tells the
compiler to allocate appropriate
memory space for the variable
based on its data type.
• The syntax for declaring a variable
is
By Technical Instructor, Mr K Chisamu 24
Examples of Variable
Declaration
By Technical Instructor, Mr K Chisamu 25
Variables
• If variables are of the same type,
they can be declared together, as
follows:
• The variables are separated by
commas.
• For example,
By Technical Instructor, Mr K Chisamu 26
Variables
• Variables often have initial values.
• You can declare a variable and
initialize it in one step.
• Consider, for instance, the
following code:
By Technical Instructor, Mr K Chisamu 27
Variable
• You can also use a shorthand
form to declare and initialize
variables of the same type
together.
• For example,
By Technical Instructor, Mr K Chisamu 28
Note on Variables
• A variable must be declared
before it can be assigned a
value.
• A variable declared in a method
must be assigned a value
before it can be used.
By Technical Instructor, Mr K Chisamu 29
Note on Variables
• Whenever possible, declare a
variable and assign its initial
value in one step.
• This will make the program easy
to read and avoid programming
errors.
By Technical Instructor, Mr K Chisamu 30
Variable Scope
• Every variable has a scope.
• The scope of a variable is the
part of the program where the
variable can be referenced.
By Technical Instructor, Mr K Chisamu 31
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 32
QUESTION 1 [12
Marks]
• Identify and fix the errors in the
following code:
By Technical Instructor, Mr K Chisamu 33
6. Assignment Statements
& Assignment Expressions
By Technical Instructor, Mr K Chisamu 34
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 35
An Assignment Statement
• An assignment statement
designates a value for a variable.
• An assignment statement can
be used as an expression in Java.
By Technical Instructor, Mr K Chisamu 36
An Assignment Statement
• After a variable is declared, you
can assign a value to it by using an
assignment statement.
By Technical Instructor, Mr K Chisamu 37
The Assignment Operator
• In Java, the equal sign (=) is used
as the assignment operator.
• The syntax for assignment
statements is as follows:
By Technical Instructor, Mr K Chisamu 38
An Assignment Statement
• An expression represents a
computation involving values,
variables, and operators that,
taking them together, evaluates to
a value.
By Technical Instructor, Mr K Chisamu 39
An Assignment Statement
• For example, consider the following
code:
By Technical Instructor, Mr K Chisamu 40
An Assignment Statement
• You can use a variable in an
expression.
• A variable can also be used in
both sides of the = operator.
• For example,
By Technical Instructor, Mr K Chisamu 41
Assignment Statement
Example
• In this assignment statement,
the result of x + 1 is assigned to x.
• If x is 1 before the statement is
executed, then it becomes 2 after
the statement is executed.
By Technical Instructor, Mr K Chisamu 42
Placement of the Variable
• To assign a value to a variable,
you must place the variable name
to the left of the assignment
operator.
• Thus, the following statement is
wrong:
By Technical Instructor, Mr K Chisamu 43
Assignment Expression
• In Java, an assignment
statement is essentially an
expression that evaluates to the
value to be assigned to the
variable on the left side of the
assignment operator.
By Technical Instructor, Mr K Chisamu 44
Assignment Expression
• For this reason, an assignment
statement is also known as an
assignment expression.
By Technical Instructor, Mr K Chisamu 45
Assignment Expression
• For example, the following
statement is correct:
• which is equivalent to
By Technical Instructor, Mr K Chisamu 46
Assignment Expression
• If a value is assigned to multiple
variables, you can use the
following syntax:
• which is equivalent to
By Technical Instructor, Mr K Chisamu 47
Compatible Data Types
• In an assignment statement, the
data type of the variable on the
left must be compatible with the
data type of the value on the
right.
By Technical Instructor, Mr K Chisamu 48
Compatible Data Types
• For example, int x = 1.0 would be
illegal, because the data type of x
is int.
• You cannot assign a double value
(1.0) to an int variable without
using type casting.
By Technical Instructor, Mr K Chisamu 49
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 50
QUESTION 1 [12
Marks]
• Identify and fix the errors in the
following code:
By Technical Instructor, Mr K Chisamu 51
7. Named Constants
By Technical Instructor, Mr K Chisamu 52
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 53
What is a constant?
• A named constant is an
identifier that represents a
permanent value.
By Technical Instructor, Mr K Chisamu 54
What is a constant?
• The value of a variable may
change during the execution of a
program, but a named constant,
or simply constant, represents
permanent data that never
changes.
By Technical Instructor, Mr K Chisamu 55
Constants
• A constant is also known as a
final variable in Java.
By Technical Instructor, Mr K Chisamu 56
Constants
• Here is the syntax for declaring a
constant:
By Technical Instructor, Mr K Chisamu 57
The final Keyword
• A constant must be declared and
initialized in the same
statement.
• The word final is a Java keyword
for declaring a constant.
• By convention, all letters in a
constant are in uppercase.
By Technical Instructor, Mr K Chisamu 58
Benefits of Using
Constants
• There are three benefits of using
constants:
1. You don’t have to repeatedly type the
same value if it is used multiple times;
2. If you have to change the constant
value, you need to change it only in a
single location in the source code;
3. A descriptive name for a constant
makes the program easy to read.
By Technical Instructor, Mr K Chisamu 59
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 60
QUESTION 1 [2
Marks]
• What are the benefits of using
constants?
• Declare an int constant SIZE with
value 20.
By Technical Instructor, Mr K Chisamu 61
8. Named Conventions
By Technical Instructor, Mr K Chisamu 62
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 63
Naming Conventions
• Sticking with the Java naming
conventions makes your
programs easy to read and avoids
errors.
By Technical Instructor, Mr K Chisamu 64
Naming Conventions
• Make sure you choose descriptive
names with straight-forward
meanings for the variables,
constants, classes, and
methods in your program.
By Technical Instructor, Mr K Chisamu 65
Naming Conventions
• As mentioned earlier, names are
case sensitive.
• Listed below are the conventions
for naming variables, methods,
and classes.
By Technical Instructor, Mr K Chisamu 66
Naming Conventions:
Variables & Methods
• Use lowercase for variables and
methods—for example, the
variables radius and area, and the
method print.
By Technical Instructor, Mr K Chisamu 67
Naming Conventions:
Variables & Methods
• If a name consists of several words,
concatenate them into one,
making the first word lowercase
and capitalizing the first letter of
each subsequent word—for
example, the variable
numberOfStudents.
By Technical Instructor, Mr K Chisamu 68
Naming Variables &
Methods
• This naming style is known as the
camelCase because the
uppercase characters in the
name resemble a camel’s humps.
By Technical Instructor, Mr K Chisamu 69
Naming Classes
• Capitalize the first letter of each
word in a class name—for example,
the class names ComputeArea
and System.
By Technical Instructor, Mr K Chisamu 70
Naming Constants
• Capitalize every letter in a
constant, and use underscores
between words—for example, the
constants PI and MAX_VALUE.
By Technical Instructor, Mr K Chisamu 71
Naming Conventions
• It is important to follow the
naming conventions to make
your programs easy to read.
By Technical Instructor, Mr K Chisamu 72
Naming Conventions
• Do not choose class names that
are already used in the Java
library.
• For example, since the System
class is defined in Java, you
should not name your class
System.
By Technical Instructor, Mr K Chisamu 73
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 74
QUESTION 1 [8
Marks]
• What are the naming conventions
for class names, method names,
constants, and variables?
• Which of the following items can be
a constant, a method, a variable,
or a class according to the Java
naming conventions?
• MAX_VALUE, Test, read, readDouble
By Technical Instructor, Mr K Chisamu 75
QUESTION 2 [4
Marks]
• Step 1: Declare a double variable
named miles with an initial value
100.
• Step 2: Declare a double constant
named KILOMETERS_PER_MILE with
value 1.609.
By Technical Instructor, Mr K Chisamu 76
QUESTION 3 [2
Marks]
• Step 3: Declare a double variable
named kilometers, multiply miles
and KILOMETERS_PER_MILE, and
assign the result to kilometers.
• Step 4: Display kilometers to the
console. What is kilometers after
Step 4?
By Technical Instructor, Mr K Chisamu 77
9. Numeric Data Types &
Operations
By Technical Instructor, Mr K Chisamu 78
Lesson Overview
1. Introduction 10. Numeric Literals
2. Writing a Simple 11. Evaluating Expressions
Program & Operator Precedence
3. Console Input 12. Augmented
4. Identifiers Assignment Operators
5. Variables 13. Increment &
Decrement Operators
6. Assignment
Statements & 14. Numeric Type
Expressions Conversions
7. Named Constants 15. Software Development
Process
8. Naming Conventions
16. Common Errors &
9. Numeric Data Types & Pitfalls
Operations
By Technical Instructor, Mr K Chisamu 79
Numeric Types
• Java has six numeric types for
integers and floating-point
numbers with operators +, -, *, /,
and %.
By Technical Instructor, Mr K Chisamu 80
Numeric Types
• Every data type has a range of
values.
• The compiler allocates memory
space for each variable or
constant according to its data
type.
By Technical Instructor, Mr K Chisamu 81
Numeric Types
• Java provides eight primitive
data types for numeric values,
characters, and Boolean values.
By Technical Instructor, Mr K Chisamu 82
Numeric Types
• This section introduces numeric
data types and operators.
By Technical Instructor, Mr K Chisamu 83
Numeric Types
• The following table lists the six
numeric data types, their
ranges, and their storage sizes.
By Technical Instructor, Mr K Chisamu 84
Numeric Data Types
By Technical Instructor, Mr K Chisamu 85
IEEE 754 Standard
• IEEE 754 is a standard approved
by the Institute of Electrical and
Electronics Engineers for
representing floating-point
numbers on computers.
By Technical Instructor, Mr K Chisamu 86
IEEE 754 Standard
• The standard has been widely
adopted.
• Java uses the
• 32-bit IEEE 754 for the float type
• 64-bit IEEE 754 for the double
type.
By Technical Instructor, Mr K Chisamu 87
IEEE 754 Standard
• The IEEE 754 standard also
defines special floating-point
values.
By Technical Instructor, Mr K Chisamu 88
Numeric Integer Types
• Java uses four types for
integers:
1. byte,
2. short,
3. int,
4. long.
By Technical Instructor, Mr K Chisamu 89
Numeric Integer Types
• Choose the type that is most
appropriate for your variable.
• For example, if you know an
integer stored in a variable is
within a range of a byte, declare
the variable as a byte.
By Technical Instructor, Mr K Chisamu 90
Numeric Floating-Point
Types
• Java uses two types for floating-
point numbers:
1. float
2. double.
By Technical Instructor, Mr K Chisamu 91
Numeric Floating-Point
Types
• The double type is twice as big as
float, so the double is known as
double precision, and float as
single precision.
By Technical Instructor, Mr K Chisamu 92
Numeric Floating-Point
Types
• Normally, you should use the
double type, because it is more
accurate than the float type.
By Technical Instructor, Mr K Chisamu 93
Reading Numbers from
the Keyboard
• You can use the methods listed in
the following table to read a
number of the
• byte,
• short,
• int,
• long,
• float type
By Technical Instructor, Mr K Chisamu 94
Reading Numbers from
the Keyboard
By Technical Instructor, Mr K Chisamu 95
Keyboard Input Examples
• The following figure shows
examples of reading input from
the Keyboard:
By Technical Instructor, Mr K Chisamu 96
By Technical Instructor, Mr K Chisamu 97
Values Out of Range
• If you enter a value with an
incorrect range or format, a
runtime error would occur.
• For example, if you enter a value
128 for line 3, an error would
occur because 128 is out of
range for a byte type integer
By Technical Instructor, Mr K Chisamu 98
Numeric Types
• The operators for numeric data
types include the standard
arithmetic operators:
1. addition (+),
2. subtraction (–),
3. multiplication (*),
4. division (/),
5. and remainder (%).
By Technical Instructor, Mr K Chisamu 99
Numeric Types
• as listed in the following table:
• The operands are the values
operated by an operator.
By Technical Instructor, Mr K Chisamu 100
Numeric Operators
By Technical Instructor, Mr K Chisamu 101
Integer Division
• When both operands of a
division are integers, the result
of the division is the quotient and
the fractional part is truncated.
By Technical Instructor, Mr K Chisamu 102
Integer Division
• For example,
• 5 / 2 yields 2, not 2.5,
• –5 / 2 yields –2, not –2.5.
By Technical Instructor, Mr K Chisamu 103
Floating-Point Division
• To perform a floating-point
division, one of the operands
must be a floating-point
number.
• For example,
• 5.0 / 2 yields 2.5.
By Technical Instructor, Mr K Chisamu 104
Modulo / Remainder
Operator
• The % operator, known as
remainder, yields the remainder
after division.
By Technical Instructor, Mr K Chisamu 105
Modulo / Remainder
Operator
• The operand on the left is the
dividend, and the operand on the
right is the divisor.
By Technical Instructor, Mr K Chisamu 106
Modulo / Remainder
Operator
• Therefore,
• 7 % 3 yields 1,
• 3 % 7 yields 3,
• 12 % 4 yields 0,
• 26 % 8 yields 2,
• 20 % 13 yields 7.
By Technical Instructor, Mr K Chisamu 107
Modulo / Remainder
Operator
• The % operator is often used for
positive integers, but it can also
be used with negative integers
and floating-point values.
By Technical Instructor, Mr K Chisamu 108
Modulo / Remainder
Operator
• The remainder is negative only if
the dividend is negative.
• For example,
• -7 % 3 yields -1,
• -12 % 4 yields 0,
• -26 % -8 yields -2,
• 20 % -13 yields 7.
By Technical Instructor, Mr K Chisamu 109
Modulo / Remainder
Operator
• Remainder is very useful in
programming.
By Technical Instructor, Mr K Chisamu 110
Modulo / Remainder
Operator
• For example, an even number %
2 is always 0 and a positive odd
number % 2 is always 1.
• Thus, you can use this property to
determine whether a number is
even or odd.
By Technical Instructor, Mr K Chisamu 111
Modulo / Remainder
Operator
• The following program obtains
minutes and remaining seconds
from an amount of time in
seconds.
• For example, 500 seconds
contains 8 minutes and 20
seconds.
By Technical Instructor, Mr K Chisamu 112
By Technical Instructor, Mr K Chisamu 113
Sample Run
By Technical Instructor, Mr K Chisamu 114
Modulo / Remainder
Operator
• The nextInt() method (line 8)
reads an integer for seconds.
• Line 10 obtains the minutes using
seconds / 60.
• Line 11 (seconds % 60) obtains the
remaining seconds after taking
away the minutes.
By Technical Instructor, Mr K Chisamu 115
Unary & Binary Operators
• The + and - operators can be both
unary and binary.
• A unary operator has only one
operand;
• a binary operator has two.
By Technical Instructor, Mr K Chisamu 116
Unary & Binary Operators
• For example, the – operator in –5
is a unary operator to negate
number 5, whereas the – operator
in 4 – 5 is a binary operator for
subtracting 5 from 4
By Technical Instructor, Mr K Chisamu 117
Exponent Operations
• The [Link](a, b) method can
be used to compute ab.
• The pow method is defined in the
Math class in the Java API.
By Technical Instructor, Mr K Chisamu 118
Exponent Operations
• You invoke the method using the
syntax [Link](a, b) (e.g.,
[Link](2, 3)), which returns
the result of ab (23).
• Here, a and b are parameters for
the pow method and the numbers
2 and 3 are actual values used to
invoke the method.
By Technical Instructor, Mr K Chisamu 119
Exponent Operations
• For example,
By Technical Instructor, Mr K Chisamu 120
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 121
QUESTION 1 [2
Marks]
• What is the result of 25 / 4?
• How would you rewrite the
expression if you wished the
result to be a floating-point
number?
By Technical Instructor, Mr K Chisamu 122
QUESTION 2 [4
Marks]
• Show the result of the following
code:
By Technical Instructor, Mr K Chisamu 123
QUESTION 3 [4
Marks]
• Are the following statements
correct?
• If so, show the output.
By Technical Instructor, Mr K Chisamu 124
QUESTION 4 [1 Mark]
• Write a statement to display the
result of 23.5.
By Technical Instructor, Mr K Chisamu 125
QUESTION 5 [2
Marks]
• Suppose m and r are integers.
• Write a Java expression for mr2
to obtain a floating-point result.
By Technical Instructor, Mr K Chisamu 126