Precedence of Operations in Java - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Precedence of Operations in Java

Description:

Strive for clarity over brevity. 5. General Guidelines ... Strive for simplicity and clarity and brevity, but there are trade-offs among these ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 17
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Precedence of Operations in Java


1
Precedence of Operations in Java
2
What is Precedence?
  • "Precedence" is the order in which multiple
    operations will take place
  • For example
  • amt 7 3 / 5
  • Double.toString(myCar.getWt() shipWt 1.1)

3
Precedence in Different Programming Languages
  • Each language has its own set of rules
  • Some languages may have just a few rules, while
    others may have many and complex rules -- there
    are complex tradeoffs here for the language
    designer
  • Java is in the middle, for example not as simple
    as Smalltalk and not as complex C
  • The rules match C more than Smalltalk

4
Precedence in Java
  • Java has a very clear set of unambiguous rules --
    there is no "guessing"!
  • Each symbol has only one meaning
  • It is possible to leave out parenthesis much of
    the time
  • Strive for clarity over brevity

5
General Guidelines
  • Java evaluates left to right, with exception
    noted below
  • Generally the basic arithmetic operations,
    logical ands and ors, and assignment operations
    are done in the order you would expect
    (FORTUNATE)
  • With the exceptions noted on a later slide, the
    following are a relatively complete precedence
    list

6
Precedence Chart
  • 1. ( ) for clarification for use in
    subscripts . the dot character which
    indicates that an attribute or message follows
  • 2. auto increment -- auto decrement
    ! not

7
Precedence Chart
  • 3. for multiplication / for all
    divisions for modulus (remainder)Note
    there is no integer division operator
  • 4. for addition - for subtraction

8
Precedence Chart
  • 5. gt for greater than gt for greater than
    or equal lt for less than lt for
    less than or equal
  • 6. for equality ! for inequality

9
Precedence Chart
  • 7. for boolean and (short cut)
  • 8. for boolean or (short cut) Note most
    programmers always use short cut and and or
    for boolean logic

10
Precedence Chart
  • 9. assignment a a ? - a a
    - ? a a ? / a a / ? a
    a ? Note this is the other category that
    is evaluated right to left (but by convention we
    don't put more than one of these in one statement)

11
Some Practice
  • Write using the shorthand
  • aVar aVar 5
  • myCtr myCtr - 18
  • thisVal 243. thisVal
  • oldBal oldBal / 2.
  • rqstAmt rqstAmt 3
  • nxtRqst 4 nxtRqst

12
Some Practice (more)
  • Write using the shorthand
  • sum sum 400.
  • myCtr myCtr 1
  • value value - 25.
  • oneBefore oneBefore - 1
  • day day 7

13
Practice (going the other way)
  • Expand the shorthand
  • nextItem
  • oneToLookAt --
  • amt 45.
  • pay funding
  • weeklyRate 1.1
  • regValue / totalValue

14
Just a thought .
  • Beginning programmers often get confused about
    whether to say
  • or
  • Why did the language designers "need" to make the
    choice to use ?

15
Precedence Chart
  • As business programmers (and beginners) will skip
    the following operators
  • bit shift (left, right, negation, etc.)
  • bitwise and bitwise or
  • conditional operator ?
  • also known as "the question/colon operator"
  • example
  • pay (hours gt 40 ? hours rate rate
    40)

16
Summary
  • You need to know the precedence order to
    understand what will happen when a Java statement
    is executed
  • to understand other's Java code
  • to write your own Java code
  • Java is unambiguous in its rules of precedence
  • You can either learn all Java's rules, or learn
    the basic ones and then use parentheses
  • Strive for simplicity and clarity and brevity,
    but there are trade-offs among these
Write a Comment
User Comments (0)
About PowerShow.com