• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help to understand basic arithmetics in Java

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose we have this expression:



now i need to know how java performs this operation.
I know the precedence but still not able to figure out.

Please explain.
Thank You
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should probably read something like this. operators all have a precedence - some get done before others, just like in math.

If i recall correctly, anything inside parens is done first, so we'd get

6*10/5*5+14/2-9+20

Then, mult/div come next, in order from left to right

60/5*5+14/2-9+20

60/5*5+14/2-9+20

12*5+14/2-9+20

60+14/2-9+20

60+7-9+20

then, addition and subtraction, in order from left to right

67-9+20
58+ 20
78



 
raul saini
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, I was performing multiple calculation at a time.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, you can, as long as you complete ALL operations of a given precedence before any others. I could have just as easily done this:

6*10/5*5+14/2-9+(10*2);

6*10/5*5+14/2-9+20; //take care of everything inside parens

60 + 7 - 9 + 20 // take care of all multiplication and division

78 // take care of all addition and subtraction.



 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that with integer types, you can encounter issues due to losing data when performing calculations, which may make it seem that Java violates rules of arithmetics. For example:
(a / b * c) = (a * c / b) may not hold:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That problem with integer arithmetic applies to most computer languages.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With floating point calculations there can even be a difference between (a+b)+c and a+(b+c).
 
reply
    Bookmark Topic Watch Topic
  • New Topic