DOC PREVIEW
UW-Madison CS 302 - CS 302 Lecture Notes

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Operator Precedence in the Java™ Programming Languagehandout for CS 302 by Will Benton (willb@cs)Operator precedence defines the order in which various operators are evaluated. (In fact, you may remember "order of operations" from secondary school algebra.)As an example, let's say we have the following line of Java code:int x = 4 + 3 * 5;The variable x gets the value of evaluating the expression 4 + 3 * 5. There are a couple of ways to evaluate that expression, though: We can either perform the addition first or perform the multiplication first. By choosing which operation to perform first, we are actually choosing between two different expressions: 1. (4 + 3) * 5 == 352. 4 + (3 * 5) == 19In the absence of parentheses, which choice is appropriate? Programming languages answer this question by defining precedence levels for each operator, indicating which is to be performed first. In the case of Java, multiplication takes precedence over addition; therefore, x will get the value 19.For arithmetic expressions, multiplication and division are evaluated before addition and subtraction, just like in mathematics. Of course, just as you might in a math class, you can always parenthesize Java expressions to indicate which are to be evaluated first. Sensible use of parentheses will make your programs easier to read even if your expressions all use the standard evaluation order.This sheet shows the operator precedences for the Java operators you'll be using most frequently in CS 302. On the reverse of this sheet is a chart of the precedence levels for every operator in the Java language, provided in case you're curious!postfix increments and decrements (e.g. x++)unary positive (+x), unary negative (-x), and logical negation (!x)prefix increments and decrements (e.g. ++x)binary arithmetic operatorsbinary comparison operatorsbinary logical operatorsassignment operatorsMultiply (*), divide (/), and modulus (%) operations are evaluated before add (+) and subtract operations (-).Comparison operations (e.g. <, >, <=) are evaluated before equality operators (e.g. ==, !=).AND operations (&&) are evaluated before OR (||) operationsevaluated laterevaluated soonerjava-operator-precedence.graffle: Created on Fri Jan 20 2006; modified on Wed Feb 21 2007; page 1 of 2Copyright © 2005-2007 Will C. Benton[] . ,x++ x-- ~x++x --x +x -x !x(X) new Xsubscript, member selection, comma (only in for loop headers)postfix increment, postfix decrement, bitwise negationprefix increment, prefix decrement, unary positive, unary negative, logical negationtypecasting, object creationleft-associativeright-associative* / %multiplication, division, modulusaddition, subtraction, string concatenationx+y x-y x+"x"<< >> >>> bitwise shiftcomparison< <= > >=instanceof runtime type compatibility== != equality and inequalitybitwise AND&bitwise XOR^bitwise OR|logical AND&&logical ORleft-associative||right-associativex ? y : z ternary (conditional)= += -= *= /= %= <<= >>= >>>=&= ^= |=assignment and compound assignmenthigher precedencelower precedenceThis chart (c) 2005-2007 William C. Benton.sources: Chan, Patrick. The Java Developers Almanac 1.4. Addison-Wesley, 2002.Gosling, James, et al. The Java Language Specification, 3e. Addison-Wesley, 2005.Note: operators with the same precedence level in an expression are evaluated based on their associativity. For example, left-associative operators group from left to right. Therefore, the expression x * y % z is equivalent to (x * y) % z.java-operator-precedence.graffle: Created on Fri Jan 20 2006; modified on Wed Feb 21 2007; page 2 of


View Full Document

UW-Madison CS 302 - CS 302 Lecture Notes

Download CS 302 Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 302 Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 302 Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?