



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 1
Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2007^2
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 3
Are the Following Legal?
int x, y;
x = y = 1; Yes. Result assigns 1 to x and to y
int x = 0, y = 1;
boolean b = false; if (b = (x <= y)){ x = y; }
Yes. Result assigns true to b and 1 to x
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 4
Other Expressions with Side
Effects
Java includes abbreviations for common forms of assignment Example: increment operations (Basically equivalent to x = x + 1 ++x âPre-incrementâ Increments x, returns the new value of x x++ âPost-incrementâ In crements x, returns the old value of x Same or Different
Compare x++ * y++ ++x * ++y ++x * y++ x++ * ++y
Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2007^5
Other Assignment Operators
Example: decrement operations (Basically equivalent to x = x - 1 --x âPre-decrementâ Decrements x, returns the new value of x x-- âPost-decrementâ De crements x, returns the old value of x General modification by constant General form: <op with=>
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 9
Should You Rely on
Precedence?
No! The only ones people can remember are âPlease Excuse My Dear Aunt Sallyâ PEMDAS Bad if ( 2 * x++ < 5 * z + 3 && -w != x / 2) Better if (2 * (x++) < ((5 * z) + 3)) && ((-w) != (x / 2))
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 10
Short-circuiting
As soon as Java knows an answer â it quits evaluating the expression. What does Java print? int x = 0, y = 1; if ((y > 1) && (++x == 0)){ --y; } System.out.println (x); 0 Why? y > 1 is false The result of && will be false, regardless of second expression Java therefore does not evaluate second expression of && This treatment of &&, || is called short-circuiting Subexpressions evaluated from left to right Evaluation stops when value of over-all expression is determined
Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2007^11
Examples What does Java print? int x = 0, y = 1; if ((y >= 1) && (++x == 0)) { --y; } System.out.println (x); 1 What does Java print? int x = 0, y = 1; if ( ((y > 1) && (++x == 0)) || ((y == 1) && (x++ == 0)) ) { --y; } System.out.println (y); System.out.println (x); 0 1
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 12
Examples (cont.)
What does Java print?
int x = 0, y = 0;
while (x++ <= 4)
y += x;
System.out.println (y);
15
CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 13
Programming with Side-Effects
Generally: Side effects in conditions are hard to understand Good programming practice
Major Goal: Strive to create the most readable and maintainable code.
Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2007^14
Primitive Types and their
Hierarchy
double float long int short byte
int x = 7.2; double y = 6; Changing to something else Further Up this list is acceptable called âWidening Conversionâ Changing to Something else Further Down this list is not acceptable called âNarrowing Conversionâ Explicit casting needed for when you want a downcast