0% found this document useful (0 votes)
28 views

Chapter 5 - Operators Arithmetic Expressions

This document discusses operators and arithmetic expressions in Java. It covers basic arithmetic operators like addition, subtraction, multiplication, division, and modulus. It explains that operators act on operands to form expressions. Division can be integer or floating based on the operands, and modulus returns the remainder of a division. Examples are provided to demonstrate how each operator works. The purpose is to understand the different arithmetic operators and how to use them to evaluate expressions in Java programs.

Uploaded by

Muhd Syazrin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Chapter 5 - Operators Arithmetic Expressions

This document discusses operators and arithmetic expressions in Java. It covers basic arithmetic operators like addition, subtraction, multiplication, division, and modulus. It explains that operators act on operands to form expressions. Division can be integer or floating based on the operands, and modulus returns the remainder of a division. Examples are provided to demonstrate how each operator works. The purpose is to understand the different arithmetic operators and how to use them to evaluate expressions in Java programs.

Uploaded by

Muhd Syazrin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 72

Chapter 5

Operators & Arithmetic Expressions

1
Objectives
 At the end of this chapter, student should be
able to:
– Know the types of basic arithmetic operators
and their order of precedence
– Develop skills in computer arithmetic

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
2
Introduction
 We use arithmetic expressions to solve most
programming problems
 Arithmetic expressions comprise of operators and
operands
 Unary operator: An operator that has one operand.
 Binary operator: An operator that has two
operands.
 There are rules for writing and evaluating
arithmetic expressions

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
3
Operator and Operand
 What are operator and operand?

Example:
Example:
W+Z

Operand Operand

Operator
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
4
Operator and Operand
 Which is which?

Operator ??
Example:
Example:
A/B

Operand??

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
5
Basic Arithmetic Operators

Addition (+) Modulus (%)

Subtraction (-) Division (/)


Multiplication (*)
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
6
Basic Arithmetic Operators
 Multiplication,
Multiplication addition and subtraction are the
simplest to use
 Division is easy, but some precautions need to be
taken
 Modulus is the one that normally confuses novices

So, let’s study in detail the Division and Modulus

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
7
Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
8
Division
Example:
Example:
W/Z

Integer Division Floating Division


 W and Z are integers  W or Z or both are floats

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
9
Integer Division

Example:
Example:
8/2=4

an integer
 the result is
also an integer
an integer

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
10
Integer Division

Example:
Example:
12 / 5 = 2

an integer
 the result is
also an integer
an integer

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
11
Floating Division

Example:
Example:
12.0 / 5 = 2.0

a float
the result is a
float
an integer

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
12
Something to ponder …
What will be the answer
if an integer is divided
by 0? How about if one
of the operands is a
negative integer?

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
13
Modulus
 It returns the remainder that occurs after
performing the division of 2 operands

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
14
Modulus

Example:
Example:
12 % 5 = 2
2 the result is the
an integer 5 12 remainder of
10 remainder
12/5
result
2

an integer

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
15
Modulus

Example:
Example:
7%3=1
2 the result is the
an integer 3 7 remainder of 7/3
remainder
result 6

an integer

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
16
Modulus Operator
 Remainder is very useful in programming. For example, an
even number % 2 is always 0 and an odd number % 2 is
always 1. So you can use this property to determine
whether a number is even or odd. Suppose today is
Saturday and you and your friends are going to meet in 10
days. What day is in 10 days? You can find that day is
Tuesday using the following expression:
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
17
Problem: Displaying Time
 Write a program that obtains minutes and
remaining seconds from seconds.

DisplayTime Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
18
Note!
 Calculations involving floating-point numbers are
approximated because these numbers are not stored with
complete accuracy. For example,
System.out.println(1.0-0.1-0.1-0.1-0.1-0.1);
displays 0.5000000000000001, not 0.5
 System.out.println(1.0 - 0.9);
displays 0.09999999999999998, not 0.1.
Integers are stored precisely. Therefore, calculations with
integers yield a precise integer result.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
19
Exponent Operations
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
20
Number Literals
 A literal is a constant value that appears
directly in the program. For example, 34,
1,000,000, and 5.0 are literals in the following
statements:

int i = 34;
long x = 1000000;
double d = 5.0;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
21
Integer Literals
 An integer literal can be assigned to an integer variable as long as
it can fit into the variable.
 A compilation error would occur if the literal were too large for
the variable to hold.
 For example, the statement byte b = 1000 would cause a
compilation error, because 1000 cannot be stored in a variable of
the byte type.
 An integer literal is assumed to be of the int type, whose value is
between -231 (-2147483648) to 231–1 (2147483647).
 To denote an integer literal of the long type, append it with the
letter L or l. L is preferred because l (lowercase L) can easily be
confused with 1 (the digit one).

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
22
Floating-Point Literals
 Floating-point literals are written with a decimal point.
 By default, a floating-point literal is treated as a double
type value.
 For example, 5.0 is considered a double value, not a float
value.
 You can make a number a float by appending the letter f
or F, and make a number a double by appending the letter
d or D.
 For example, you can use 100.2f or 100.2F for a float
number, and 100.2d or 100.2D for a double number.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
23
double vs. float
 The double type values are more accurate than the
float type values. For example,
System.out.println("1.0 / 3.0 is " + 1.0 / 3.0);

displays 1.0 / 3.0 is 0.3333333333333333

16 digits

System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F);

displays 1.0F / 3.0F is 0.33333334


7 digits

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
24
Scientific Notation
 Floating-point literals can also be specified in
scientific notation, for example, 1.23456e+2, same as
1.23456e2, is equivalent to 123.456, and 1.23456e-2
is equivalent to 0.0123456.
 E (or e) represents an exponent and it can be either in
lowercase or uppercase.

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
25
Something to ponder …
The earlier expressions
contain only one
operator at a time. What
if the expression
contains more than one
operator?

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
26
Arithmetic Expression
 An expression may contain 2 or more
arithmetic operators
 Main issue:

ORDER OF PRECEDENCE

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
27
Arithmetic Expression
Examples:
Examples:
5+6 = 11
5+6*2 = 17
22 or 17?
2.5 + 6 – 2 * 2 = ??
12 / 6.0 – 2 * 2 = ??
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
28
Arithmetic Expression
Order of Precedence:

High: * / %
Low: + -

All operators have a precedence level.


High precedence level operators are evaluated before
lower ones.
Operators of the same precedence level are evaluated
from left to right

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
29
Arithmetic Expression

Example:
Example:
2.5 + 6 – 2 * 2 = 4.5
??

2.5 + 6 – 4

8.5 – 4

4.5
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
30
Try it!
Example:
Example:
12 + 6.0 – 2 * 2 = ??

What’s the answer??

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
31
Arithmetic Expression
 All expressions in parentheses (brackets) must be
evaluated prior to values outside brackets
 Nested parenthesized expressions must be evaluated
from the inside out, with the innermost expression
evaluated first
Example:
Example:
( 9 – ( 3 + 2 ) ) * 3 = ??

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
32
Arithmetic Expression
Example:
Example:

 (( 99 –– ((–343 ++ 225)) )) ** 33 == ??
12
??
12

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
33
Assignment Statement
 There are 3 types of assignment:
– Simple
– Multiple
– Shorthand

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
34
Simple Assignment
Syntax:

variable = expression ;

Don’t forget the semicolon !!

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
35
Buying
Buying price:
price: 10.00
10.00 Buying price: 10.00
_
Discount
Discount rate:
rate: 0.25
0.25 Simple Assignment
Discount
_ rate: _
0.25
For buying price RM10.00 and discount rate 0.25
ForImport
buyingjava.utils.Scanner;
Import price RM10.00 and discount
java.utils.Scanner; _ rate 0.25
The total price is RM7.50
_ public class discount {
_ public class discount {
public
publicstatic
staticvoid
voidmain(String
main(String[][]args
args)){{
Scanner
Scanner inin==new
newScanner(System.in);
Scanner(System.in); price 10.00??
float
floatprice,
price,discount,
discount,total;
total;
System.out.print(“Buying ??
0.25
System.out.print(“Buyingpriceprice::“);
“); discount
price
price==in.nextDouble();
in.nextDouble();
System.out.print(“Discount
System.out.print(“Discountrate rate:: “);
“); total 7.50
??
discount=in.nextDouble();
discount=in.nextDouble();
total
total==price
price––(price
(price**discount);
discount);
System.out.println(“For
System.out.println(“Forbuying
buyingprice
price”+ ”+price
price++““and
anddiscount
discount
rate”
rate”++discount);
discount);
System.out.println(“The
System.out.println(“Thetotal
totalprice
priceisis“+“+total);
total);
}}
}}

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
36
Multiple Assignment
Syntax:

variable = variable = expression ;

Don’t forget the semicolon !!

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
37
Multiple Assignment
Example:
Example: ??
0
number
int
int number,
number, total;
total; ??
0
total
float
float start_x,
start_x, start_y;
start_y;
100.0
??
start_x
.. .. ..
100.0
??
number
number == total
total == 0;
0; start_y

start_x
start_x == start_y
start_y == 100.0;
100.0;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
38
Augmented Assignment
Operators
Syntax:

variableX = variableX op expression ;


variableX op= expression;

Also called augmented assignment operator

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
39
Augmented Assignment
Operators

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
40
Augmented Assignment
Operators
 Whenever the expression on the right
contains the variable on the left (to which
the value is assigned)
Example:
Example:
num
num +=
+= 5;
5; num
20
15

num
num == num
num ++ 5;
5; 15 + 5
20

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
41
Shorthand Assignment
 Expressions can also be stated using
shorthand assignment operators
Example:
Example:
num
num += 5; similar to
+= 5; num = num + 5

shorthand assignment operator

Shorthand assignment operators have the lowest order of


precedence – the last one to be evaluated
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
42
Shorthand Assignment

Operation Examples of Description


expression
+= num += 5; num = num + 5;

-= num -= 5; num = num – 5;

*= num *= 5; num = num * 5;

/= num /= 5; num = num / 5;

%= num %= 5; num = num % 5;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
43
Shorthand Assignment
Example:
Example:
pay
pay +=
+= hour
hour ** rate
rate ** 22
similar to pay = pay + (hour * rate * 2)
100.00
180.00
pay
 pay + (hour * rate * 2)
8
 pay + (8 * 5.00 * 2) hour
 100.00 + 80.00 rate
5.00

 180.00

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
44
Relational Operators
Operation Description Examples of Value
Expression
< Less than 6<9 true
<= Less than or equal to 5 <= 5 true
> Greater than 2>6 false

>= Greater than or equal to 9 >= 5 true

== Equal to 7 == 5 false

!= Not equal to 6 != 5 true

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
45
Mantic/Logical Operators
Symbol Description
&& AND
|| OR
! NOT

a b a && b a || b !a !b
T T T T F F
T F F T F T
F T F T T F
F F F F T T

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
46
Compound Statement
 Arithmetic, relational and mantic operators can be
integrated/combined in one expression

Example:
Example: 2
a
!! (( cc >> aa )) 15
c
 ! ( 15 > 2 )

 ! ( true )

 false

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
47
Compound Statement
Example:
Example:
(a
(a >=
>= 1)
1) &&
&& (b
(b ==
== 5)
5)
 ( 2 >= 1 ) && ( b == 5 ) a 2

 true && ( b == 5 ) b 5

 true && ( 5 == 5 ) c 15

 true && true 17


d
 true

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
48
Compound Statement
Example:
Example:
(c
(c >=
>= (( bb ** 33 )) )) |||| (a
(a ==
== 3)
3)
2
a
5
b
15
c
17
d

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
49
Compound Statement
Example:
Example:
!! (( (( aa << bb )) |||| (( cc >> dd )) ))

2
a
5
b
15
c
17
d

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
50
Increment and Decrement
 This operation contains only one operand, that is, the
operand which value will be incremented/decremented
Symbol Description Examples of Description
Expression
++ Increment i++ i=i+1
operand by 1
-- Decrement i-- i=i–1
operand by 1

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
51
Prefix and Postfix
 Increment and Decrement operators can be either in
prefix or postfix forms
Expression Description
i++ Value of i is incremented after being used in the
expression
++i Value of i is incremented before being used in the
expression
i-- Value of i is decremented after being used in the
expression
--i Value of i is decremented before being used in the
expression

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
52
Increment and Decrement
Example:
Example:
int
int num;
num;
System.out.println(“Key-in
System.out.println(“Key-in aa number:
number: “);
“);
26
27
??
num=in.nextInt();
num=in.nextInt(); num
System.out.println(“Value
System.out.println(“Value before
before being
being
incremented:
incremented: ”” ++ num);
num);
num++;
num++;
System.out.println(“Value
System.out.println(“Value after
after being
being
incremented: ” + num);
incremented: ” + num);

Key-in a number: 26
_
Value
_ before being incremented: 26
_
Value after being incremented: 27 _
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
53
Prefix and Postfix
Example:
Example:
jj == i++
i++ -- 22
similar to
5
6
j = i – 2; i
i = i + 1; j
??
3

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
54
Prefix and Postfix
Example:
Example:
jj == ++i
++i -- 22
similar to
5
6
i = i + 1; i
j = i – 2; j
??
4

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
55
Something to ponder …
The earlier expressions
contain only one type at
a time. What if the
expression contains
more than one type?

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
56
Conversion Rules
 When performing a binary operation involving two
operands of different type, Java automatically converts the
operand based on the following rules:
– If one of the operands is double, the other is converted into
double
– Otherwise, if one of the operands is float, the other is
converted to float
– Otherwise, if one of the operands is long, the other is
converted to long
– Otherwise, both operands are converted to int

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
57
Conversion Problems Example

byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
58
Type Conversion (Casting)
 Used to avoid implicit type coercion.
 Syntax:
(dataTypeName) expression
 Expression evaluated first, then type converted
to: dataTypeName
 Examples:
– (int)(7.9 + 6.7) = 14
– (int)(7.9) + (int)(6.7) = 13

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
59
Type Conversion (Casting)
Implicit casting
double d = 3; (type widening)

Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)

What is wrong? int x = 5 / 2.0;


range increases

byte, short, int, long, float, double

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
60
Type Conversion (Casting)
 Casting is used to compute a value that is equivalent to its
operand’s value (based on the stated data type)

Example:
Example:
int
int xx == (int)
(int) 3.0;
3.0; //
// type
type narrowing
narrowing
int
int yy == (int)
(int) 65.76;//truncation
65.76;//truncation
65
y
3
x

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
61
Type Casting Example

Example:
Example:
int
int x;x;
float
float yy == 35.87;
35.87; 35.87
xx == (int) y
(int) y;
y;
35
??
 ( int ) 35.87 x

 35

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
62
Problem: Keeping Two Digits
After Decimal Points
 Write a program that displays the sales tax
with two digits after the decimal point.

SalesTax Run

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
63
Casting in an Augmented
Expression
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. Therefore, the following code is correct.
int sum = 0;
sum += 4.5; // sum becomes 4 after this statement

sum += 4.5 is equivalent to sum = (int)(sum + 4.5).

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
64
Exercise

1.
1. What
What isis wrong?
wrong?
int
int ii == 5;
5;
double
double dd == 2.0;2.0;
int
int xx == i/d;
i/d;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
65
Exercise
2.
2. Assume
Assume that
that int
int aa == 10
10 and
and double
double dd == 10.0,
10.0,
and
and that
that each
each expressions
expressions isis independent.
independent. What
What
are
are the
the results
results of
of the
the following
following expressions?
expressions?
a)a
a)a == 25
25 // 4;
4;
b)a
b)a == 15
15 %
% 99 ++ 22 ** 55 –– 3;
3;
c)d
c)d +=
+= 4.3
4.3 ** 22 ++ (a--);
(a--);
d)d
d)d -=
-= 6.2
6.2 ** 44 ++ ++a;
++a;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
66
Exercise
3. What is the result of the following code:
3. What is the result of the following code:

a)
a) b)
b)
byte
byte x;x; byte
byte x;x;
int
int y;
y; int
int y;
y;
byte
byte z;
z; int
int z;
z;

xx==50;
50; xx==100;
100;
yy==10;
10; yy==100;
100;
zz==xx++y;
y; zz==xx++y;
y;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
67
Exercise
4. What is the result of the following code:
4. What is the result of the following code:

a)
a) b)
b)
int
intx;
x; byte
byte x;
x;
double
double y;
y; double
double y;y;
float
floatz;
z; double
double z;
z;

xx==50;
50; xx==1000;
1000;
yy==10.0;
10.0; yy==1000.0;
1000.0;
zz==xx++y;
y; zz==xx++y;
y;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
68
Common Error 1:
Undeclared/Uninitialized Variables
and Unused Variables
double interestRate = 0.05;
double interest = interestrate * 45;

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
69
Common Error 2: Integer Overflow

int value = 2147483647 + 1;


// value will actually be -2147483648

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
70
Common Error 3: Round-off Errors

System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);

System.out.println(1.0 - 0.9);

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
71
Common Error 4: Unintended Integer
Division
int number1 = 1; int number1 = 1;
int number2 = 2; int number2 = 2;
double average = (number1 + number2) / 2; double average = (number1 + number2) / 2.0;
System.out.println(average); System.out.println(average);

(a) (b)

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
72

You might also like