• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about the precedence of &&, ||

 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
&& is higher than || in precedence
while in the following:
boolean b = (ex1) || (ex2) && (ex3);
only ex1 is executed (ex1 resovled to be true)
Confused.
Does anyone can give some help?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:Does anyone can give some help?

I'm pretty sure this thread will clear your doubts.

If you still have questions/doubts after very carefully reading the aforementioned thread, just reply to one of the threads and let us know!
 
Tim Cooke
Sheriff
Pie
Posts: 3203
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suppose that operator precedence does not correlate directly to order of execution. I would propose that to be an implementation of the JVM, which may differ between JVM products and versions.

Edit: Or what Roel said. He'd know better than me.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Jamoba Black wrote:Does anyone can give some help?

I'm pretty sure this thread will clear your doubts.

If you still have questions/doubts after very carefully reading the aforementioned thread, just reply to one of the threads and let us know!


I have read the thread, I think the main logic is clear.
this code helps a lot:

I might understand it as:
precedence decides how different parts of expression combine, we can use () to include them
After () is placed, the evaluation is always from left to right. (except assignment)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:I have read the thread, I think the main logic is clear.

Glad to hear that thread was helpful.

Jamoba Black wrote:this code helps a lot:

I wonder how that code snippet is related to your original question about operator precedence of the short-circuit operators

And this adjusted code snippet illustrates your reasoning is spot-on!Output:
10 * 2 = 20
10 + 20 = 30
30


Hope it helps!
Kind regards,
Roel

PS. that's a good code example to practice the order of evaluation, but I would not write such code in any (real world) application
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Jamoba Black wrote:I have read the thread, I think the main logic is clear.

Glad to hear that thread was helpful.

Jamoba Black wrote:this code helps a lot:

I wonder how that code snippet is related to your original question about operator precedence of the short-circuit operators

And this adjusted code snippet illustrates your reasoning is spot-on!Output:
10 * 2 = 20
10 + 20 = 30
30


Hope it helps!
Kind regards,
Roel

PS. that's a good code example to practice the order of evaluation, but I would not write such code in any (real world) application


It's getting more clear to me. The original i is like instance variable, add and multiply method use local variable i to calculate. I like this logic.
Thank you so much!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:The original i is like instance variable, add and multiply method use local variable i to calculate.

The original i is still a local variable, not an instance variable.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Jamoba Black wrote:The original i is like instance variable, add and multiply method use local variable i to calculate.

The original i is still a local variable, not an instance variable.


Yes, you are right!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic