Program Control
Program Control
95-713
Sakir YUCEL
MISM/MSIT
Carnegie Mellon University
Program Control
5 2
After assignment n1 = n2
reference variables Number objects
n1 n2 n2.i n1.i
5 2
“Aliasing” In Function Calls
public class PassObject {
static void f(Number m) {
m.i = 15;
}
public static void main(String[] args) {
Number n = new Number();
n.i = 14;
f(n); // what is n.i now?
}
}
Math Operators
+, -, *, /, %
Integer division truncates, i.e., 16/3 = 5
Modulus operator returns remainder on integer
division, i.e., 16%3 = 1
Shorthand:
x += 4; is the same as
x = x + 4;
This works for the other arithmetic operators as well.
Auto Increment and Decrement
++ increases by one, and -- decreases by one.
Two flavors of each: pre and post:
int i = 1, j;
j = i++; // j = 1, i = 2
j = ++i; // j = 3, i = 3
j = i--; // j = 3, i = 2
j = --i; // j = 1, i = 1
Booleans and Relational Operators
The boolean type has two possible values,
true and false.
The relational operators >, >=, <, <=, ==
and != produce a boolean result.
>, >=, <, <= are legal for all built-in types
except booleans, == and != are legal for all.
Testing for (Non-)Equivalence
The == and != operators need to be used with
care with objects.
public class Equivalence {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2); // prints false
System.put.println(n1 != n2); // prints true
}
}
The equals( ) Operator
This exists for all objects (don’t need it for
built-in types).
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2); // prints true
The equals( ) Operator (cont.)
But exists doesn’t necessarily mean properly
defined!
class Number {
int i;
}
:
Number n1 = new Number();
Number n2 = new Number();
n1.i = 3;
n2.i = 3;
System.out.println(n1.equals(n2)); // prints false
The equals( ) Operator (cont.)
The equals( ) operator is properly defined for
most Java library classes.
The default behavior is to compare references,
so…
When you define a class, if you’re planning to
use equals( ), you need to define it (i.e.,
override the default behavior).
Logical Operators
These are AND (&&), OR (||), and NOT (!).
These work on booleans only; if you have old
“C” habits, forget them!
Use parentheses freely to group logical
expressions.
Logical expressions short-circuit; as soon as
the result is known, evaluation stops.
Short-Circuiting Example
public class ShortCircuit {
static boolean test1(int val) {return val < 1;}
static boolean test2(int val) {return val < 2;}
static boolean test3(int val) {return val < 3;}
public static void main(String[] args) {
if (test1(0) && test2(2) && test3(2))
System.out.println(“Expression is true”);
else
System.out.println(“Expression is false”);
}
}
Bitwise, Shift, Ternary Operators
Bitwise & shift operators manipulate
individual bits in integral primitive types.
The ternary if-else operator looks like this:
boolean-expression ? value0 : value1
The result is either value0 or value1,
depending on the truth of the boolean.
The String + Operator
The + operator is “overloaded” for String
objects; it means concatenation.
It reminds me of good old C++…
If an expression begins with a String, then all
the following operands of + will be converted
into Strings:
int x = 0, y = 1, z = 2;
String myString = “x, y, z ”;
System.out.println(myString + x + y + z);
A Useful Figure
char
float double
do
statement_or_block // evaluate last
while (boolean_expression)
Example:
for (int i = 0; i < myArray.size(); i++) {
myArray[i] = 0;
}
Break and Continue