0% found this document useful (0 votes)
3 views58 pages

Chapter 2 Operators and Statements

The document provides an overview of Java operators, including their types (unary, binary, ternary) and how expressions are evaluated. It discusses numeric promotion rules, logical operators, assignment, casting, and the implications of overflow and underflow. Additionally, it covers control flow statements such as if-then, switch, while, do-while, and for loops, along with examples and common pitfalls.

Uploaded by

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

Chapter 2 Operators and Statements

The document provides an overview of Java operators, including their types (unary, binary, ternary) and how expressions are evaluated. It discusses numeric promotion rules, logical operators, assignment, casting, and the implications of overflow and underflow. Additionally, it covers control flow statements such as if-then, switch, while, do-while, and for loops, along with examples and common pitfalls.

Uploaded by

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

III

1. a special symbol that can be applied

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

type as its promoted operands.


Rules
Numeric
Promotion
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Rules
Numeric
Promotion
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Working

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

3. The last statement does not compile because you cannot


take the logical complement of a numeric value, nor can
you assign an integer to a boolean variable.
III
C
Keep an eye out for questions that use the

)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

long t = 192301398193810323; // DOES NOT COMPILE


Casting primitives is required any time you are

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

short z = x * y; // DOES NOT COMPILE


Overflow is when a number is so large that it will no longer

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

2. Comparing two boolean values.


3. Comparing two objects, including null and String
values
Equality
Pr
M og Operators
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Statement
The if-then
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
else
Statement
The if-then-
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
Statement
The switch
Pr
M og
ar ra
co m
Yi min
m g
ya (J
em A
VA
)N
C
III
int dayOfWeek = 5;

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

Note that boolean and long, and


Pr

their associated wrapper classes, are not


supported by switch statements
III
C
)N
VA
The while

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

evaluate to true. The result is that the loop will never


Pr

end, creating what is commonly referred to as an


infinite loop.
III
C
)N
VA
The do-while

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

Enter three numbers: 2 2 2


Pr

All numbers are equal


Write a Java program that prompts the user to enter

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

Sum of the first 5 positive integers: 15


Write a Java program that prompts the user to enter

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

Sum of the first 5 positive integers: 15


Write a program that takes an integer input from the

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

Notice that in the second version, there is no update statement as it is not


required when using the java.util.Iterator class
Labels
Adding
Optional
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
The break
Statement
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
The break
Statement
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
Statement
The continue
Pr
M og
ar ra
co m
Yi min
m g
ya J
em AV
A
N
C
III
Statement
The continue
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
Be able to write code that uses Java operators. This chapter covered a wide
variety of operator symbols. Go back and review them several times so that
you are familiar with them throughout the rest of the book. Be able to

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

You might also like