Java Programming
Fundamentals
Elementary Programming
By Kiswick Chisamu
14. Numeric Type
Conversions
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
Numeric Type Conversions
• Floating-point numbers can be
converted into integers using
explicit casting.
By Technical Instructor, Mr K Chisamu 4
Numeric Type Conversions
• If an integer and a floating-point
number are involved in a binary
operation, Java automatically
converts the integer to a
floating-point value.
By Technical Instructor, Mr K Chisamu 5
Numeric Type Conversions
• Therefore, 3 * 4.5 is the same as
3.0 * 4.5.
• You can always assign a value to a
numeric variable whose type
supports a larger range of
values;
By Technical Instructor, Mr K Chisamu 6
Numeric Type Conversions
• For instance, you can assign a long
value to a float variable.
• You cannot, however, assign a
value to a variable of a type with
a smaller range unless you use
type casting.
By Technical Instructor, Mr K Chisamu 7
Type Casting
• Casting is an operation that
converts a value of one data
type into a value of another
data type.
By Technical Instructor, Mr K Chisamu 8
Type Casting
• Casting a type with a small
range to a type with a larger
range is known as widening a
type.
By Technical Instructor, Mr K Chisamu 9
Type Casting
• Casting a type with a large
range to a type with a smaller
range is known as narrowing a
type.
• Java will automatically widen a
type, but you must narrow a type
explicitly.
By Technical Instructor, Mr K Chisamu 10
Numeric Type Conversions
• The syntax for casting a type is
to specify the target type in
parentheses, followed by the
variable’s name or the value to
be cast.
By Technical Instructor, Mr K Chisamu 11
Numeric Type Conversions
• For example, the following
statement
• displays 1.
• When a double value is cast into
an int value, the fractional part
is truncated.
By Technical Instructor, Mr K Chisamu 12
Numeric Type Conversions
• The following statement
• displays 0.5, because 1 is cast to
1.0 first, then 1.0 is divided by 2.
By Technical Instructor, Mr K Chisamu 13
Numeric Type Conversions
• However, the statement
• displays 0, because 1 and 2 are both
integers and the resulting value
should also be an integer.
By Technical Instructor, Mr K Chisamu 14
Caution! Possible Loss of
Precision
• Casting is necessary if you are
assigning a value to a variable of a
smaller type range, such as
assigning a double value to an int
variable.
By Technical Instructor, Mr K Chisamu 15
Caution! Possible Loss of
Precision
• A compile error will occur if
casting is not used in situations of
this kind.
• However, be careful when using
casting, as loss of information
might lead to inaccurate results.
By Technical Instructor, Mr K Chisamu 16
Note
• Casting does not change the
variable being cast.
• For example, d is not changed after
casting in the following code:
By Technical Instructor, Mr K Chisamu 17
Casting: Augmented
Expressions
• In Java, an augmented expression
of the form x1 op= x2 is
implemented as x1 = (T) (x1 op
x2), where T is the type for x1.
By Technical Instructor, Mr K Chisamu 18
Casting: Augmented
Expressions
• Therefore, the following code is
correct:
By Technical Instructor, Mr K Chisamu 19
Explicit Casting
• To assign a variable of the int type
to a variable of the short or byte
type, explicit casting must be
used.
By Technical Instructor, Mr K Chisamu 20
Explicit Casting
• For example, the following
statements have a compile error:
By Technical Instructor, Mr K Chisamu 21
Permissible Range
• However, so long as the integer
literal is within the permissible
range of the target variable,
explicit casting is not needed to
assign an integer literal to a
variable of the short or byte type
By Technical Instructor, Mr K Chisamu 22
An Example
• The following program displays the
sales tax with two digits after the
decimal point.
By Technical Instructor, Mr K Chisamu 23
[Link]
By Technical Instructor, Mr K Chisamu 24
Sample Run
By Technical Instructor, Mr K Chisamu 25
…cont’d
• The variable purchaseAmount is
197.55.
• The sales tax is 6% of the
purchase, so the tax is evaluated
as 11.853.
By Technical Instructor, Mr K Chisamu 26
…cont’d
• Note
By Technical Instructor, Mr K Chisamu 27
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 28
QUESTION 1 [1 Mark]
• Can different types of numeric
values be used together in a
computation?
By Technical Instructor, Mr K Chisamu 29
QUESTION 2 [2
Marks]
• What does an explicit casting
from a double to an int do with
the fractional part of the double
value?
• Does casting change the variable
being cast?
By Technical Instructor, Mr K Chisamu 30
QUESTION 3 [2
Marks]
• Show the following output:
By Technical Instructor, Mr K Chisamu 31
QUESTION 4 [2
Marks]
• If you change (int)(tax * 100) /
100.0 to (int)(tax * 100) / 100,
what will be the output for the
input purchase amount of
197.556?
By Technical Instructor, Mr K Chisamu 32
QUESTION 5 [2
Marks]
• Show the output of the following
code:
By Technical Instructor, Mr K Chisamu 33
QUESTION 6 [2
Marks]
• Write an expression that rounds up
a double value in variable d to an
integer.
By Technical Instructor, Mr K Chisamu 34
15. Software Development
Process
By Technical Instructor, Mr K Chisamu 35
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 36
Software Development
Process
• The software development life
cycle is a multistage process that
includes requirements
specification, analysis, design,
implementation, testing,
deployment, and maintenance
By Technical Instructor, Mr K Chisamu 37
Software Development
Process
• Developing a software product is
an engineering process.
By Technical Instructor, Mr K Chisamu 38
Software Development
Process
• Software products, no matter how large
or how small, have the same life cycle:
• requirements specification,
• analysis,
• design,
• implementation,
• testing,
• deployment,
• and maintenance, as shown in figure below
By Technical Instructor, Mr K Chisamu 39
By Technical Instructor, Mr K Chisamu 40
…cont’d
• At any stage of the software
development life cycle, it may
be necessary to go back to a
previous stage to correct errors
or deal with other issues that might
prevent the software from
functioning as expected.
By Technical Instructor, Mr K Chisamu 41
Requirements
Specification
• Requirements specification is a
formal process that seeks to
understand the problem the
software will address, and to
document in detail what the
software system needs to do.
• This phase involves close
interaction between users and
developers.
By Technical Instructor, Mr K Chisamu 42
System Analysis
• System analysis seeks to analyze
the data flow and to identify the
system’s input and output.
• When you perform analysis, it
helps to identify what the output
is first, then figure out what input
data you need in order to produce
the output.
By Technical Instructor, Mr K Chisamu 43
System Design
• System design is to design a
process for obtaining the output
from the input.
• This phase involves the use of
many levels of abstraction to
break down the problem into
manageable components and
design strategies for implementing
each component.
By Technical Instructor, Mr K Chisamu 44
System Design
• You can view each component as
a subsystem that performs a
specific function of the system.
• The essence of system analysis
and design is input, process,
and output (IPO).
By Technical Instructor, Mr K Chisamu 45
System Implementation
• Implementation involves
translating the system design into
programs.
By Technical Instructor, Mr K Chisamu 46
System Implementation
• Separate programs are written
for each component then
integrated to work together.
• This phase requires the use of a
programming language such as
Java.
By Technical Instructor, Mr K Chisamu 47
System Implementation
• The implementation involves
• coding,
• self-testing,
• and debugging (that is, finding
errors, called bugs, in the code).
By Technical Instructor, Mr K Chisamu 48
System Testing
• Testing ensures the code meets
the requirements specification
and weeds out bugs.
By Technical Instructor, Mr K Chisamu 49
System Testing
• An independent team of software
engineers not involved in the
design and implementation of
the product usually conducts such
testing.
By Technical Instructor, Mr K Chisamu 50
System Deployment
• Deployment makes the software
available for use.
• Depending on the type of
software, it may be installed on
each user’s machine, or installed
on a server accessible on the
Internet.
By Technical Instructor, Mr K Chisamu 51
System Maintenance
• Maintenance is concerned with
updating and improving the
product.
By Technical Instructor, Mr K Chisamu 52
System Maintenance
• A software product must
continue to perform and improve in
an ever-evolving environment.
By Technical Instructor, Mr K Chisamu 53
System Maintenance
• This requires periodic upgrades
of the product to fix newly
discovered bugs and incorporate
changes.
By Technical Instructor, Mr K Chisamu 54
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 55
QUESTION 1 [2
Marks]
• How would you write the following
arithmetic expression?
By Technical Instructor, Mr K Chisamu 56
16. Common Errors &
Pitfalls
By Technical Instructor, Mr K Chisamu 57
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 58
Common Errors & Pitfalls
• Common elementary
programming errors often
involve
• undeclared variables,
• uninitialized variables,
• integer overflow,
• unintended integer division,
• round-off errors.
By Technical Instructor, Mr K Chisamu 59
Common Error 1
• Undeclared/Uninitialized
Variables and Unused
Variables.
• A variable must be declared with
a type and assigned a value
before using it.
By Technical Instructor, Mr K Chisamu 60
Common Error 1
• A common error is not declaring a
variable or initializing a variable.
Consider the following code:
By Technical Instructor, Mr K Chisamu 61
Common Error 2
(Integer Overflow)
• Numbers are stored with a limited
numbers of digits.
• When a variable is assigned a
value that is too large (in size) to
be stored, it causes overflow.
By Technical Instructor, Mr K Chisamu 62
Common Error 2
(Integer Overflow)
• For example, executing the
following statement causes
overflow, because the largest
value that can be stored in a
variable of the int type is
2147483647.
By Technical Instructor, Mr K Chisamu 63
Common Error 2
(Integer Overflow)
• 2147483648 will be too large for
an int value.
• Likewise, executing the following
statement also causes overflow,
because the smallest value that
can be stored in a variable of the
int data type is -2147483648.
By Technical Instructor, Mr K Chisamu 64
Common Error 2
(Integer Overflow)
• -2147483649 is too large in size
to be stored in an int variable.
By Technical Instructor, Mr K Chisamu 65
Common Error 2
(Integer Overflow)
• Java does not report warnings or
errors on overflow, so be careful
when working with integers close
to the maximum or minimum
range of a given type.
By Technical Instructor, Mr K Chisamu 66
Common Error 2
(Integer Underflow)
• When a floating-point number is
too small (i.e., too close to zero) to
be stored, it causes underflow.
• Java approximates it to zero, so
normally you don’t need to be
concerned about underflow.
By Technical Instructor, Mr K Chisamu 67
Common Error 3
(Round-off Errors)
• A round-off error, also called a
rounding error, is the difference
between the calculated
approximation of a number and its
exact mathematical value.
By Technical Instructor, Mr K Chisamu 68
Common Error 3
(Round-off Errors)
• For example, 1/3 is approximately
0.333 if you keep three decimal
places, and is 0.3333333 if you
keep seven decimal places.
By Technical Instructor, Mr K Chisamu 69
Common Error 3
(Round-off Errors)
• Since the number of digits that can
be stored in a variable is limited,
round-off errors are inevitable.
By Technical Instructor, Mr K Chisamu 70
Common Error 3
(Round-off Errors)
• Calculations involving floating-
point numbers are approximated
because these numbers are not
stored with complete accuracy.
By Technical Instructor, Mr K Chisamu 71
Common Error 3
(Round-off Errors)
• For example,
• displays 0.09999999999999998,
not 0.1.
By Technical Instructor, Mr K Chisamu 72
…cont’d
• Integers are stored precisely.
• Therefore, calculations with
integers yield a precise integer
result.
By Technical Instructor, Mr K Chisamu 73
Common Error 4
(Unintended Integer
Division)
• Java uses the same divide
operator, namely /, to perform both
integer and floating-point
division.
By Technical Instructor, Mr K Chisamu 74
Common Error 4
(Unintended Integer
Division)
• When two operands are integers,
the / operator performs an integer
division.
• The result of the operation is an
integer.
• The fractional part is truncated.
By Technical Instructor, Mr K Chisamu 75
Common Error 4
(Unintended Integer
Division)
• To force two integers to perform a
floating-point division, make
one of the integers into a floting-
point number.
By Technical Instructor, Mr K Chisamu 76
Common Error 4
(Unintended Integer
Division)
• For example, the code in (a)
displays that average as 1 and the
code in (b) displays that average
as 1.5.
By Technical Instructor, Mr K Chisamu 77
Common Error 4
(Unintended Integer
Division)
By Technical Instructor, Mr K Chisamu 78
Common Pitfall 1
(Redundant Input Objects)
• New programmers often write
the code to create multiple input
objects for each input.
By Technical Instructor, Mr K Chisamu 79
Common Pitfall 1
(Redundant Input Objects)
• For example, the following code
reads an integer and a double
value:
By Technical Instructor, Mr K Chisamu 80
Common Pitfall 1
(Redundant Input Objects)
• The code is not good.
• It creates two input objects
unnecessarily and may lead to
some subtle errors.
By Technical Instructor, Mr K Chisamu 81
Common Pitfall 1
(Redundant Input Objects)
• You should rewrite the code as
follows:
By Technical Instructor, Mr K Chisamu 82
REVIEW QUESTIONS
By Technical Instructor, Mr K Chisamu 83
QUESTION 1 [2
Marks]
• Can you declare a variable as int
and later re-declare it as double?
By Technical Instructor, Mr K Chisamu 84
QUESTION 2 [2
Marks]
• What is an integer overflow?
• Can floating-point operations
cause overflow?
By Technical Instructor, Mr K Chisamu 85
QUESTION 3 [1 Mark]
• Will overflow cause a runtime
error?
By Technical Instructor, Mr K Chisamu 86
QUESTION 4 [3
Marks]
• What is a round-off error?
• Can integer operations cause
round-off errors?
• Can floating-point operations
cause round-off errors?
By Technical Instructor, Mr K Chisamu 87
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 88
LESSON SUMMARY
By Technical Instructor, Mr K Chisamu 89
Lesson Summary
Identifiers are names for naming
elements such as variables,
constants, methods, classes,
and packages in a program.
By Technical Instructor, Mr K Chisamu 90
Lesson Summary
An identifier is a sequence of characters
that consists of letters, digits,
underscores (_), and dollar signs ($).
An identifier must start with a letter or
an underscore.
It cannot start with a digit.
An identifier cannot be a reserved
word. An identifier can be of any
length.
By Technical Instructor, Mr K Chisamu 91
Lesson Summary
Variables are used to store data
in a program.
To declare a variable is to tell
the compiler what type of data a
variable can hold.
By Technical Instructor, Mr K Chisamu 92
Lesson Summary
There are two types of import
statements: specific import and
wildcard import.
The specific import specifies a
single class in the import
statement.
The wildcard import imports all
the classes in a package.
By Technical Instructor, Mr K Chisamu 93
Lesson Summary
In Java, the equal sign (=) is
used as the assignment
operator.
By Technical Instructor, Mr K Chisamu 94
Lesson Summary
A variable declared in a method
must be assigned a value before it
can be used.
By Technical Instructor, Mr K Chisamu 95
Lesson Summary
A named constant (or simply a
constant) represents permanent
data that never changes.
A named constant is declared by
using the keyword final.
By Technical Instructor, Mr K Chisamu 96
Lesson Summary
Java provides four integer
types:
1. byte,
2. short,
3. int,
4. long
that represent integers of four
different sizes.
By Technical Instructor, Mr K Chisamu 97
Lesson Summary
Java provides two floating-point
types:
1. float
2. Double
that represent floating-point
numbers of two different
precisions.
By Technical Instructor, Mr K Chisamu 98
Lesson Summary
Java provides operators that
perform numeric operations:
+ (addition),
– (subtraction),
* (multiplication),
/ (division),
% (remainder).
By Technical Instructor, Mr K Chisamu 99
Lesson Summary
Integer arithmetic (/) yields an
integer result.
By Technical Instructor, Mr K Chisamu 100
Lesson Summary
The numeric operators in a Java
expression are applied the same
way as in an arithmetic
expression.
By Technical Instructor, Mr K Chisamu 101
Lesson Summary
Java provides the augmented
assignment operators:
+= (addition assignment),
–= (subtraction assignment),
*= (multiplication assignment),
/= (division assignment),
%= (remainder assignment).
By Technical Instructor, Mr K Chisamu 102
Lesson Summary
The increment operator (++)
and the decrement operator (––)
increment or decrement a variable
by 1.
By Technical Instructor, Mr K Chisamu 103
Lesson Summary
When evaluating an expression
with values of mixed types, Java
automatically converts the
operands to appropriate types.
By Technical Instructor, Mr K Chisamu 104
Lesson Summary
You can explicitly convert a
value from one type to another
using the (type)value notation.
By Technical Instructor, Mr K Chisamu 105
Lesson Summary
Casting a variable of a type with
a small range to a type with a
larger range is known as
widening a type.
By Technical Instructor, Mr K Chisamu 106
Lesson Summary
Casting a variable of a type with
a large range to a type with a
smaller range is known as
narrowing a type.
By Technical Instructor, Mr K Chisamu 107
Lesson Summary
Widening a type can be
performed automatically without
explicit casting.
Narrowing a type must be
performed explicitly.
By Technical Instructor, Mr K Chisamu 108
Lesson Summary
In computer science, midnight of
January 1, 1970, is known as the
UNIX epoch.
By Technical Instructor, Mr K Chisamu 109
THE END
By Techincal Instructor, Mr K Chisamu 110