Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
• Knute Snortum
Sheriffs:
• Liutauras Vilda
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Joe Ess
• salvin francis
• fred rosenberger

# Operator Precedence in Java

Ranch Hand
Posts: 114
What is the value of the expression : 3+4-5*2/7 ?

I tried to solve it manually as follows. Since operator precedence is checked from LEFT to RIGHT of an expression, Multiplication(*) will happen first, followed by DIVIDE (/), followed by ADD(+) and finally SUBTRACT (-)

Here are the steps.
3+4-5*2/7
3+4-10/7
3+4-1.4
7-1.4
5.6

However, when I try to run the same in Java, I get the answer as 6.0

Can anyone explain, how operator precedence works?

Ranch Hand
Posts: 107
Without seeing your program I cannot be positive what is going on here, but I suspect it you are not using float or double variables and you are ending up with a rounded answer.

Also just a tip, which I'm sure you are aware of, adding parenthesis to complex math code makes code significantly more readable and more difficult to make mistakes.

Ranch Hand
Posts: 2066
Check the below code:

Normally, numbers are defined as int in java. So,

In the 3rd line, that isn't 1.4 but 1. So the ultimate answer is 6.

Ranch Hand
Posts: 200

Bobby Smallman wrote:Without seeing your program I cannot be positive what is going on here, but I suspect it you are not using float or double variables and you are ending up with a rounded answer.

Also just a tip, which I'm sure you are aware of, adding parenthesis to complex math code makes code significantly more readable and more difficult to make mistakes.

It's quite obvious that if using constants here, JVM takes pure int-arithmetic. That's why 5*2/7 is 1.

Operators are checked from left to right BUT with checking precedence!

First it's 5*2 => 10. Then 10/7 => 1 (because it's int!). Then 3 + 4 - 1 => 6.

If / was over * it would be 3 + 4 + (5*(2/7)) => 3 + 4 + (5*0) => 7

Ranch Hand
Posts: 114
I seemed to have forgotten the basic rule that when we use two integers for an arithmetic operation, the result is always an INT!

Thanks!

However, I am unable to understand

Operators are checked from left to right BUT with checking precedence!

What do you mean by checking precedence?

Christian Dillinger
Ranch Hand
Posts: 200
The are operators having a higher "value" than others, that precendence. So the JVM takes that statement "3+4-5*2/7" and tries to calculate... It divides the statement into smaller parts. And where does it divide if it has more than one possibility? It takes the operators with the lower "value".

So it should look like this:
3+(4-5*2/7)

Next step: resolve the part inside braces.

=> 3 + ( 4 + (5*2/7))

and so on.

May be this is a bad example because the "error" was just because of int-arithmetic.

Take 3*4/2+1/4*5.

Lowest "value" is +. So you get (3*4/2) + (1/4*5). Now you only have ops that have the same "value". They are checked from left to right. => (3*4)/2 and (1/4)*5.

Here can find a table with the operators and their "value": http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

 There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once. Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous