• Post Reply Bookmark Topic Watch Topic
  • New Topic

Precedence of operators vs remainder  RSS feed

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very simple question, can you please open my eyes for below, I guess it should be 10%40 for row below, where I'm wrong?

System.out.println(int1%int2*(int3+int1)/int2);
System.out.println(10%40);

20
10
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is your question?

You can find a table that shows the precedence of operators in Oracle's Java Tutorials: Operators. As you can see in the table, % has the same precedence as multiplication * and division /. Expressions are evaluated from left to right, so if you have an expression with multiple operators that have the same precedence, then the one on the left will be evaluated first.

So a line such as: int1 % int2 * (int3 + int1) / int2

will be interpreted as: ((int1 % int2) * (int 3 + int1)) / int2

Note that you didn't tell us what the values of the variables int1, int2, int3 is so we can't explain you with those values what exactly happens.
 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I sorted out the problem,
values are int1=10, int2=20, int3=30

Jesper de Jong wrote:What exactly is your question?

You can find a table that shows the precedence of operators in Oracle's Java Tutorials: Operators. As you can see in the table, % has the same precedence as multiplication * and division /. Expressions are evaluated from left to right, so if you have an expression with multiple operators that have the same precedence, then the one on the left will be evaluated first.

So a line such as: int1 % int2 * (int3 + int1) / int2

will be interpreted as: ((int1 % int2) * (int 3 + int1)) / int2

Note that you didn't tell us what the values of the variables int1, int2, int3 is so we can't explain you with those values what exactly happens.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!