• Post Reply Bookmark Topic Watch Topic
  • New Topic

operator precedence in java  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone


well I understand the above code
what I observe is operator precedence has
got no significance in case of any expressions

I will have to always start from left to right(as per Associativity)
Only Associativity has to be considered always while serving this type
of questions.
If operator precedence has no significance then why they have
given it in documentation?.

Please rectify if I'm wrong


thnks in advance
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If it is evaluated from left to right without operator precedence, you expect it prints 9?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go! A page that states what operator precedence you can see in Java.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read what the Java Language Specification says about the && and || operators.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Tong wrote:
If it is evaluated from left to right without operator precedence, you expect it prints 9?


Agreed. Just because you find an expression where precedence doesn't seem to have effect, it doesn't mean that there is no effect.

Also, the OP seems to be mixing evaluation order and associativity. Keep in mind that precedence, associativity, and evaluation order are three different things -- and all three should be understood when evaluating expressions.

Henry
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there is a fourth consideration, which you find in the JLS link I posted earlier: & | and && || behave differently.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!