Chapter 2 Operators and Statements
Chapter 2 Operators and Statements
C
to a set of variables, values, or
)N
literals—referred to as operands—
VA
Understanding
and that returns a result.
em A
ya (J
2. Three flavors of operators - unary,
m g
Java Operators
Yi min
binary, and ternary
co m
ar ra 3. Java expression is actually evaluated
M og
from right-to-left given the specific
Pr
operators involved
Order of
operator
precedence
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
1. If two values have different data types,
III
C
Java will automatically promote one of
)N
Numeric the values
VA
em A
2. If one of the values is integral and the
ya (J
Promotion
m g
other is floating-point, Java will
Yi min
Rules co m
ar ra
M og automatically promote the integral
value to the floating-point value’s data
Pr
type.
3. Smaller data types, namely byte, short,
III
C
and char, are first promoted to int any
)N
time they’re used with a Java binary
Numeric
VA
arithmetic operator, even if neither of the
em A
ya (J
Promotion operands is int.
m g
Yi min
4. After all promotion has occurred and the
Rules co m
ar ra
M og
operands have the same data type, the
resulting value will have the same data
Pr
Operators
with Unary
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
The logical complement operator, !, flips the value of
III
Logical
a boolean expression. For example, if the value is
C
)N
true, it will be converted to false, and vice versa. To
Complement
VA
illustrate this, compare the outputs of the following
em A
statements:
ya (J
and
m g
Yi min
Negation co m
ar ra
M og
Operators
Pr
Likewise, the negation operator, -, reverses the sign of
III
Logical
a numeric expression, as shown
C
)N
in these statements:
Complement
VA
em A
ya (J
and
m g
Yi min
Negation co m
ar ra
M og
Operators
Pr
III
Logical
C
)N
Complement
VA
em A
1. The first statement will not compile due the fact that in
ya (J
and Java you cannot perform a logical inversion of a numeric
m g
Yi min
value.
Negation
2. The second statement does not compile because you
co m
ar ra cannot numerically negate a boolean value; you need to
M og
Operators
use the logical inverse operator.
Pr
)N
logical complement operator or numeric
VA
em A
values with boolean expressions or variables.
ya (J
m g
Unlike some other programming languages, in
Yi min
co m
Java 1 and true are not related in any way,
ar ra
M og
just as 0 and false are not related.
Pr
and
Operators
Increment
Decrement
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
a binary operator that modifies, or assigns, the
III
variable on the left-hand side of the operator, with
C
the result of the value on the right-hand side of
)N
the equation. The simplest assignment operator is
VA
the = assignment, which you have seen already:
Assignment
em A
ya (J
m g
int x = 1;
Operators
Yi min
co m
int x = 1.0; // DOES NOT COMPILE
ar ra
short y = 1921222; // DOES NOT COMPILE
M og
int z = 9f; // DOES NOT COMPILE
Pr
III
going from a larger numerical data type to a
C
smaller numerical data type, or converting from a
)N
Casting
floating-point number to an integral value.
VA
em A
ya (J
Primitive
int x = (int)1.0;
short y = (short)1921222; // Stored as 20678
m g
Yi min
int z = (int)9l;
Values
long t = 192301398193810323L;
co m
ar ra
short x = 10;
M og
short y = 3;
Pr
III
fit within the data type, so the system “wraps around” to
C
the next lowest value and counts up from there. There’s
)N
also an analogous underflow, when the number is too low
Overflow
VA
to fi t in the data type.
For example, the following statement outputs a negative number:
em A
ya (J
and System.out.print(2147483647+1); // -2147483648
m g
Yi min
Underflow
Since 2147483647 is the maximum int value, adding any strictly
co m
positive value to it will cause it to wrap to the next negative number.
ar ra
M og
Pr
Operators
Compound
Assignment
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Operators
Relational
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Operators
Relational
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
III
C
)N
VA
Logical
em A
ya (J
m g
Operators
Yi min
Here are some tips to help remember this table:
AND is only true if both operands are true.
co m
ar ra
Inclusive OR is only false if both operands are false.
M og
Exclusive OR is only true if the operands are different.
Pr
The equals operator == and not equals operator !=
III
they compare two operands and return a boolean value
C
)N
about whether the expressions or values are equal, or not
equal, respectively
VA
Equality
em A
ya (J
The equality operators are used in one of three scenarios:
m g
1. Comparing two numeric primitive types. If the
Operators
Yi min
numeric values are of different data types, the values
co m
are automatically promoted as previously described.
ar ra For example, 5 == 5.00 returns true since the left side
M og
is promoted to a double.
Pr
III
switch(dayOfWeek) {
C
)N
default:
VA
System.out.println("Weekday");
The switch
em A
break;
ya (J
m g
case 0:
Statement
Yi min
System.out.println("Sunday");
co m
ar ra break;
M og
case 6:
Pr
System.out.println("Saturday");
break;
}
• int and Integer
III
• byte and Byte
C
Data types
)N
• short and Short
VA
• char and Character
supported
em A
ya (J
• String
m g
by switch
Yi min
• enum values
statements co m
ar ra
M og
em A
ya (J
m g
Statement
Yi min
int roomInBelly = 5;
co m
public void eatCheese(int bitesOfCheese) {
ar ra while (bitesOfCheese > 0 && roomInBelly > 0) {
M og
bitesOfCheese--;
Pr
roomInBelly--;
}
System.out.println(bitesOfCheese+" pieces of cheese left");
}
III
Consider the following segment of code:
C
int x = 2;
)N
int y = 5;
VA
while(x < 10)
Infinite
em A
y++;
ya (J
You may notice one glaring problem with this
m g
Loops
Yi min
statement: it will never end! The Boolean expression
co m
that is evaluated prior to each loop iteration is never
ar ra
modified, so the expression (x < 10) will always
M og
em A
ya (J
m g
Statement
Yi min
co m
ar ra
M og
int x = 0;
Pr
do {
x++;
} while(false);
System.out.println(x); // Outputs 1
vs.
while
When to Use
do-while Loops
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
QnA
)N
C
III
The for
Statement
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
III
C
)N
VA
for(int i = 0; i < 10; i++) {
em A
ya (J
System.out.print(i + " ");
m g
Yi min
}
co m
ar ra
M og
Pr
III
C
for( ; ; ) {
)N
Creating
VA
System.out.println("Hello World");
em A
an Infinite
ya (J
}
m g
Yi min
Loop co m
ar ra
M og
Pr
III
C
int x = 0;
)N
Adding for(long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
VA
Multiple System.out.print(y + " ");
em A
ya (J
}
m g
Terms to the
Yi min
System.out.print(x);
for Statement co m
ar ra
M og
Pr
III
Redeclaring a
C
int x = 0;
)N
for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) { //
VA
Variable in DOES NOT COMPILE
em A
ya (J
the System.out.print(x + " ");
m g
Yi min
}
Initialization co m
ar ra
Block
M og
Pr
Using
III
C
for(long y = 0, int x = 4; x < 5 && y<10; x++, y++) {
)N
Incompatible // DOES NOT COMPILE
VA
Data Types in System.out.print(x + " ");
em A
ya (J
}
m g
the
Yi min
Initialization co m
ar ra
M og
Block
Pr
III
C
for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
)N
Using Loop System.out.print(y + " ");
VA
Variables }
em A
ya (J
System.out.print(x); // DOES NOT COMPILE
m g
Outside the
Yi min
Loop co m
ar ra
M og
Pr
Write a program that takes in three numbers from
III
the user and outputs the largest number.
C
)N
VA
Enter three numbers: 7 12 3
em A
ya (J
m g
The largest number is 12
Yi min
co m
ar ra
M og
III
C
a positive integer n and prints the sum of the first n
)N
VA
positive integers.
em A
ya (J
m g
Yi min
Sample Input/Output:
co m
ar ra
Enter a positive integer: 5
M og
Pr
III
C
a positive integer n and prints the sum of the first n
)N
VA
positive integers.
em A
ya (J
m g
Yi min
Sample Input/Output:
co m
ar ra
Enter a positive integer: 5
M og
Pr
III
user and uses a for loop to generate the following
C
)N
pattern
VA
em A
1
ya (J
22
m g
Yi min
333
4444
co m
55555 ar ra
M og
666666
Pr
7777777
88888888
999999999
III
C
)N
VA
Write a program that generates a multiplication
em A
ya (J
m g
table for the numbers 1 through 10
Yi min
co m
ar ra
M og
Pr
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
QnA
)N
C
III
Statement
The for-each
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
Since for and for-each both use the same keyword, you might be wondering how
they are related. While this discussion is out of scope for the exam, let’s take a
moment to explore how for-each loops are converted to for loops by the
III
compiler. When for-each was introduced in Java 5, it was added as a compile-
C
Comparing for
N
time enhancement. This means that Java actually converts the for-each loop
A
into a standard for loop during compilation. For example, assuming names is an
em AV
array of String[] as we saw in the first example, the following two loops are
ya J
and for-each
equivalent
m g
Yi min
co m
Loops
ar ra
M og
Pr
For objects that inherit java.lang.Iterable, there is a different, but similar,
conversion.
For example, assuming values is an instance of List<Integer>, as we saw in the
III
second example, the following two loops are equivalent:
C
Comparing for
N
A
em AV
ya J
and for-each
m g
Yi min
co m
Loops
ar ra
M og
Pr
III
recognize which operators are associated with which data types. Some
C
N
operators may be applied only to numeric primitives, some only to boolean
A
values, and some only to objects. It is important that you notice when an
Exam
em AV
operator and operand(s) are mismatched, as this issue is likely to come up
ya J
in a couple of exam questions.
m g
Yi min
Essentials
co m
Understand Java operator precedence. Most Java operators you’ll work
ar ra
with are binary, but the number of expressions is often greater than two.
M og
Therefore, you must understand the order in which Java will evaluate each
Pr
operator symbol.
Be able to write code that uses parentheses to override operator
precedence. You can use parentheses in your code to manually change the
order of precedence.
Understand if and switch decision control statements. The if-then and
switch statements come up frequently throughout the exam in questions
unrelated to decision control, so make sure you fully understand these
III
basic building blocks of Java.
C
N
Understand loop statements. Know the syntactical structure of all loops,
A
including while, do-while, and for. Each loop has its own special properties
Exam
em AV
and structures. Also, be familiar with the enhanced for-each loops that
ya J
iterate over lists.
m g
Yi min
Essentials
Understand how break and continue can change flow control. Know how to
co m
change the flow control within a statement by applying a break or continue
ar ra
command. Also know which control statements can accept break
M og
statements and which can accept continue statements. Finally, understand
Pr
how these statements work inside embedded loops or switch statements.
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
QnA
III