Lecture Set 4 More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short circuiting CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr main method public static void main String args statements here All projects and examples have defined this method No explicit call needed Parts of the line Name main Parameter List String args Return type void Access public more on this later CMSC 131 Fall 2010 Jan Plane Ben Modifier static Bederson adapted from Bonnie Dorr MethodStars java Other public static methods A static method is associated with a class Must have all of the same parts as the main not an individual instance object public static body returnType name argList For example defining a method to print a number of stars public static void printStars int count for int curr 0 curr count curr count 1 System out print For example defining a method to print a number of stars printStars 3 System out println printStars 77 CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 1 method information parameters and arguments parameter list argument list type name for each item in the list e g MyGrid grid char where expression e g grid t for each item in the list Matched between the arguments and the parameters based on position in the list CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr PrimesMethods java Non main static public methods defining invoking and commenting Defined based on a name and a list of parameters public static void name parameterlist body Invoked by stating its name and giving an argument for each element of the parameter list name argumentlist Each method must have a well defined purpose That information goes into a comment before the method definition Each parameter s purpose should be explained Return value s purpose should be explained CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr Expressions Java expressions that yield values e g x x 1 y x y z 0 foo equals cat Expressions have values of a specific type int boolean etc Expressions can be assigned to variables appear inside other expressions etc CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 2 Expressions and Side Effects Some expressions can also alter the values of variables x 1 is an expression e g x 1 Yes Value is result of evaluation right hand side of It also alters the value of x Such alterations are called side effects CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr E1OpExpr java 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 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 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 increment x then return it x Post increment Increments x returns the old value of x return x then increment it Same or Different x x x x Compare x x x x y y y y always true never true CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 3 E2PrePostCompare java 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 Decrements x returns the old value of x return x then decrement it General modification by constant General form var op with Examples x 2 equivalent to x 2 equivalent to x 2 equivalent to x 2 equivalent to constant x x x x x 2 x 2 x 2 x 2 CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr Precedence Explains how to evaluate expressions What is value of 1 2 3 4 Precedence rules answer this question Higher precedence operators evaluated first Example from math Please Excuse my Dear Aunt Sally or PEMDAS Multiple and divide higher precedence before you add and subtract lower precedence Java follows Aunt Sally s Rules but what about other operators CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr Java Precedence Rules parentheses unary ops multiply divide add subtract comparisons equality logical and logical or assignments x x x x x x x these are right to left associative Higher precedence on top CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 4 Examples x y z Same as x y z x y y z w z Same as 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 CMSC 131 Fall 2010 Jan Plane Ben 11 Bederson adapted from Bonnie Dorr Should You Rely on Precedence No The only ones people can remember are Please Excuse My Dear Aunt Sally PEMDAS And maybe unary and increment decrement operators Bad if 2 x 5 z 3 w x 2 Better if 2 x 5 z 3 w x 2 Best if 2 x 5 z 3 w x 2 CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr Short circuiting Example 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 CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 5 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 x System out println y 1 CMSC 131 Fall 2010 Jan Plane Ben 0 Bederson adapted from Bonnie Dorr Examples cont What does Java print int x 0 y 0 while x 4 y x System out println y 15 CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr Programming with Side Effects Generally Side effects in conditions are hard to understand Good programming practice Conditions should be side effect free Side effects should be in stand alone statements Major Goal Strive to create the most readable and maintainable code CMSC 131 Fall 2010 Jan Plane Ben Bederson adapted from Bonnie Dorr 6 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 …
View Full Document