Java Expressions, Operators, and Precedence in CMSC 131 Spring 2009 - Prof. Bonnie J. Dorr, Study notes of Computer Science

This document from the cmsc 131 spring 2009 course covers various aspects of expressions, operators, and precedence in java programming. It explains arithmetic operators, side effects, operator precedence, short-circuiting, methods, and java precedence rules. The document also discusses expressions, side effects, and precedence examples, as well as the importance of not relying solely on precedence and the concept of short-circuiting.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-5bd-1
koofers-user-5bd-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 131 Spring 2009
Jan Plane (adapted from Bonnie Dorr)
Lecture Set 4:
More About Methods and
More About Operators
More arithmetic operators
Operator Side effects
Operator Precedence
Short-circuiting
Methods
Definitions
Invocations
CMSC 131 Spring 2009
Jan Plane (adapted from Bonnie Dorr)
1
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 Spring 2009
Jan Plane (adapted from Bonnie Dorr)
2
Expressions and Side Effects
Some expressions can also alter the values
of variables
e.g. x=1
x=1 is an expression?
Yes!
Value is result of evaluation right-hand side of =
It also alters the value of x
Such alterations are called side effects
pf3
pf4
pf5

Partial preview of the text

Download Java Expressions, Operators, and Precedence in CMSC 131 Spring 2009 - Prof. Bonnie J. Dorr and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)

Lecture Set 4:

More About Methods and

More About Operators

 More arithmetic operators

 Operator Side effects

 Operator Precedence

 Short-circuiting

 Methods

 Definitions

 Invocations

CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr) 1

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.

Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2009^2

Expressions and Side Effects

 Some expressions can also alter the values

of variables

e.g. x=

 x=1 is an expression?

 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 Spring 2009 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 2009 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

 x == x++

 x == ++x

 Compare  x++ * y++  ++x * ++y  ++x * y++  x++ * ++y

always true

never true

Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2009^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=>  Examples x += 2 equivalent to x = x+ x -= 2 equivalent to x = x- x = 2 equivalent to x = x x /= 2 equivalent to x = x/

CMSC 131 Spring 2009 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 2009 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 2009^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 2009 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 2009 Jan Plane (adapted from Bonnie Dorr) 13

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.

Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2009^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 to go lower in the list

Other public static methods

 A static method is associated with a class

 not an individual instance (object)

 Must have all of the same parts as the main

public static returnType name(argList){ body }

 For example – defining a method to print a number of stars

public static void printStars(int count){ for (int curr = 0; curr < count; curr++){ System.out.print(“*”); } }  For example – defining a method to print a number of stars printStars(3) System.out.println(); printStars(77); CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)

CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr) 19

method information:

parameters and arguments

 parameter list

 type name for each item in the list

 e.g. (MyGrid grid, char where)

 argument list

 expression for each item in the list

 e.g. (grid, ‘t’)

 Matched between the arguments and the parameters based on position in the list

Jan Plane (adapted from Bonnie Dorr)^ CMSC 131 Spring 2009^20

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