1CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture Set 4:Evaluation OrderToday:More assignment operatorsPrecedence and short-circuitingCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1ExpressionsJava “phrases” that yield valuese.g.xx + 1 - yx == y && z == 0foo.equals (“cat”)Expressions have values (int, boolean, etc.)Expressions can be assigned to variables, appear inside other expressions, etc.CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)2Expressions and Side EffectsSome expressions can also alter the values of variablese.g. x=1x=1 is an expression?Yes!Value is result of evaluation right-hand side of =It also alters the value of xSuch alterations are called side effects2CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3Are the Following Legal?int x, y;x = y = 1;Yes. Result assigns 1 to x and to yint x = 0, y = 1;boolean b = false;if (b = (x <= y)){ x = y;}Yes. Result assigns true to b and 1 to xCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4Other Expressions with Side EffectsJava includes abbreviations for common forms of assignmentExample: increment operations (Basically equivalent to x = x + 1++x “Pre-increment”Increments x, returns the new value of xx++ “Post-increment”Increments x, returns the old value of xSame or Differentx == x++x == ++xComparex++ * y++ ++x * ++y ++x * y++ x++ * ++yalways truenever trueCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Other Assignment OperatorsExample: decrement operations (Basically equivalent to x = x - 1--x “Pre-decrement”Decrements x, returns the new value of xx-- “Post-decrement”Decrements x, returns the old value of xGeneral modification by constantGeneral form: <var> <op with=> <constant>Examplesx += 2 equivalent to x = x+2x -= 2 equivalent to x = x-2x *= 2 equivalent to x = x*2x /= 2 equivalent to x = x/23CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6PrecedenceExplains how to evaluate expressionsWhat is value of 1 – 2 + 3 * 4?Precedence rules answer this questionHigher-precedence operators evaluated firstExample from math: “Please, Excuse my Dear Aunt Sally” or PEMDASMultiple and divide (higher precedence) before you add and subtract (lower precedence)Java follows “Aunt Sally’s Rules” … but what about other operators?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7Java Precedence Rulesparentheses: ( )unary ops: +x -x ++x –-x x++ x-- !xmultiply/divide: * / %add/subtract: + -comparisons: < > <= >=equality: == !=logical and: &&logical or: ||assignments: = += *= /= %= (only these are right to left associative)increasing precedenceCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8Examplesx * y + -zEquivalent to (x*y) + (-z)(x <= y && y <= z || w > z)Equivalent to ((x <= y) && (y <= z)) || (w > z)What is value of 1 – 2 + 3 * 4?1 -2 + 3 * 4= (1-2) + (3*4)= (1-2) + 12= -1 + 12= 114CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Should You Rely on Precedence?No!The only ones people can remember are “Please Excuse My Dear Aunt Sally”PEMDASBadif (2 * x++ < 5 * z + 3 && -w != x / 2)Better if (2 * (x++) < ((5 * z) + 3)) && ((-w) != (x / 2))CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10Short-circuitingAs 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);0Why?y > 1 is falseThe result of && will be false, regardless of second expressionJava therefore does not evaluate second expression of &&This treatment of &&, || is called short-circuitingSubexpressions evaluated from left to rightEvaluation stops when value of over-all expression is determinedCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)11ExamplesWhat does Java print?int x = 0, y = 1;if ((y >= 1) && (++x == 0)) {--y;}System.out.println (x);1What 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);015CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)12Examples (cont.)What does Java print?int x = 0, y = 0;while (x++ <= 4)y += x;System.out.println (y);15CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)13Programming with Side-EffectsGenerally:Side effects in conditions are hard to understandGood programming practiceConditions should be side-effect-freeSide effects should be in “stand-alone statements”Major Goal: Strive to create the most readable and maintainable code.CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)14Primitive Types and their Hierarchydoublefloatlongintshortbyteint x = 7.2;double y = 6;Changing to something else Further Up this list is acceptablecalled “Widening Conversion”Changing to Something else Further Down this list is not acceptable called “Narrowing Conversion”Explicit casting needed for when you want a downcast6CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)15Type CastingWhich of the following are legal?int x = 3.5;Illegal: 3.5 is not an intfloat x = 3;Legal: 3 is an int, which is also a floatlong i = 3;Legal: 3 is an int, which is also a longbyte x = 155;Illlegal: 155 is to big to be a byte (> 127)double d = 3.14159F;Legal: 3.14159F is a float, which is also a doubleCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)16Mixed ExpressionsWhat is result offloat x = 3 / 4;x assigned value 0.0FWhy?3, 4 are intsSo integer / operation is used, yielding 0, before upcasting is performedTo get floating point result, use explicit castingfloat x = (float) 3 / (float) 4;Assigns x the value 0.75FCan also do followingfloat x = (float) 3 / 4;Why?(float) 3 returns a value type float (3.0F)4 is an intIn this case, Java compiler uses widening conversion on “lower” type (here, int) to obtain values in same type before computing
View Full Document