Setting Up The Java Environment
Java Environment
Overveiw Of Java Programming
Java Data Types
Java Operators
Java Flow Control

Exam Objectives Covered


Operators


Expressions


Arithmetic Operators


+ Can Do Addition

byte a = 3;
byte b = 4;
byte sum = a + b;           // ERROR: result is int, must downcast
byte sumOK = (byte) a + b;  // OK

+ Can Do String Concatenation

String s = "2";
int r = 2;
System.out.println(r + s);  // what will the output be?


Modulo

int two = 11 % 3 
   // 3 fits into 11 a total of 3 times, leaving 2 as a remainder
int one = 29 % 2 
   // 2 fits into 29 a total of 14 times, leaving 1 as a remainder
int six = 13 % 7 
   // 7 fits into 13 a total of 1 time, leaving 6 as a remainder
int number = 7;

int evenOrOdd = number % 2;

Lab 5.1 - Math Magic


Increment and Decrement

int x;
int y = 0;
x = y++;    // what is the value of x after this line? ("post")
int x;
int y = 0;
x = ++y;    // what is the value of x after this line? ("pre")


Lab 5.2 - Incrementation


Relational Operators

int age = 34;
boolean isMinor = (age < 21);  // () not required, but looks cleaner


Conditional Operators


Operator Precedence

int a = 2 + 9 * 3;    

// "a" equals 29 and not 33 because multiplication has higher precedence than addition
int a = (2 + 9) * 3;    

// "a" equals 33 because parenthesis have higher precedence than 
// multiplication

Operator Precedence

precedence

Assignment - Revisited



Lab 5.3 - Experimenting with Precedence

int magic = [your number] [all the operations];
System.out.println(magic);


Primitive Type Conversion and Casting

int   + long   -> long        // int converted to long
float + double -> double      // float converted to double
int   + float  -> float       // int converted to float
float length = 2.1F;           // 2.1F is shorthand for (float) 2.13
float width = 4.51F;
double area = length * width;     // auto upcast to double

float approxArea = (float) area;  // explicit downcast to float

Implicit Upcasting

byte  + byte  -> int      // bytes converted to int
short + short -> int      // shorts converted to int
short + byte  -> int      // byte and short converted to int
byte  + byte  -> (byte) byte    // bytes converted back to byte
short + short -> (short) short  // shorts converted back to short
short + byte  -> (short) short  // byte and short converted to short

Loss of Precision

double high = 1.9;
double low = 1.1;

int newHigh = (int) high;    // "newHighRange" equals 1
int newLow = (int) low;      // "newLowRange" equals 1

Exam Objectives Covered
Operators
Expressions
Arithmetic Operators
+ Can Do Addition
+ Can Do String Concatenation
Modulo
Lab 5.1 - Math Magic
Increment and Decrement
Lab 5.2 - Incrementation
Relational Operators
Conditional Operators
Operator Precedence
Assignment - Revisited
Lab 5.3 - Experimenting with Precedence
Primitive Type Conversion and Casting
Implicit Upcasting
Loss of Precision