Java Operator Precedence

download Java Operator Precedence

of 2

Transcript of Java Operator Precedence

  • 8/13/2019 Java Operator Precedence

    1/2

    Operator Precedence in the JavaProgramming Language

    handout for CS 302 by Will Benton (willb@cs)

    Operator precedencedefines theorder in which various operatorsare evaluated. (In fact, you may

    remember "order of operations"from secondary school algebra.)

    As an example, let's say we havethe following line of Java code:

    int x = 4 + 3 * 5;

    The variable xgets the value ofevaluating the expression4 + 3 * 5. There are a coupleof ways to evaluate thatexpression, though: We can

    either perform the addition firstor perform the multiplicationfirst.

    By choosing which operation toperform first, we are actuallychoosing between two different

    expressions:

    1. (4 + 3) * 5 == 352. 4 + (3 * 5) == 19

    In the absence of parentheses,which choice is appropriate?Programming languages answerthis question by definingprecedence levels for eachoperator, indicating which is to

    be performed first. In the case ofJava, multiplication takes

    precedence over addition;therefore, xwill get the value 19.

    For arithmetic expressions,multiplication and division areevaluated before addition andsubtraction, just like inmathematics. Of course, just as

    you might in a math class, youcan always parenthesize Javaexpressions to indicate which

    are to be evaluated first.

    Sensible use of parentheseswill make your programseasier to read even if yourexpressions all use thestandard evaluation order.

    This sheet shows the operatorprecedences for the Javaoperators you'll be using mostfrequently in CS 302. On thereverse of this sheet is a chart of

    the precedence levels for everyoperator in the Java language,provided in case you're curious!

    postfix increments and decrements (e.g. x++)

    unary positive (+x), unary negative (-x), andlogical negation (!x)

    prefix increments and decrements (e.g. ++x)

    binary arithmetic operators

    binary comparison operators

    binary logical operators

    assignment operators

    Multiply (*), divide (/), andmodulus (%) operations areevaluated before add (+)and subtract operations (-).

    Comparison operations (e.g. ,

  • 8/13/2019 Java Operator Precedence

    2/2

    [] . ,

    x++ x-- ~x

    ++x --x +x -x !x

    (X) new X

    subscript, member selection, comma(only in forloop headers)

    postfix increment, postfixdecrement, bitwise negation

    prefix increment, prefixdecrement, unary positive,

    unary negative, logical

    negation

    typecasting, object creation

    left-associative

    right-associative

    * / %multiplication, division,

    modulus

    addition, subtraction, stringconcatenation

    x+y x-y x+"x"

    > >>> bitwise shift

    comparison< >=

    instanceof runtime type compatibility

    == != equality and inequality

    bitwise AND&

    bitwise XOR^

    bitwise OR|

    logical AND&&

    logical OR

    left-associative

    ||

    right-associative

    x? y: z ternary (conditional)

    =+= -= *= /= %=

    = >>>=&= ^= |=

    assignment and compoundassignment

    highe

    rprecedence

    lowerprecedence

    Thischart(c)2005-

    2007WilliamC.

    Benton.

    sources:

    Chan,

    Patrick.TheJ

    avaDevelopersAlmanac1.4.

    Addison-We

    sley,

    2002.

    Gosling,

    James,

    etal.TheJavaLanguageSpecification,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 % zis equivalent to (x * y) % z.

    java-operator-precedence.graffle: Created on Fri Jan 20 2006; modified on Wed Feb 21 2007; page 2