• Post Reply Bookmark Topic Watch Topic
  • New Topic

Short Circuit &&  RSS feed

 
priya kandasamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I know the reason for the answer of the following code

1. System.out.println(false&&false||true);
Ans: true

2. System.out.println(false&&false|true);
Ans: false
How the first one is coming as true since it is a short circuit &&? Why doesn't it exit after evaluating short circuit && as false?

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please check the precedences of the operators, which you will find here. That should explain the difference.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short-circuiting doesn't change the value. It's just a mechanism that makes things more efficient by stopping evaluation once you know what the final value is going to be. Unless one of the expressions has side-effects (e.g. changes the value of a variable) then there's no difference in the result between short-circuiting and not short-circuiting. As Campbell says, this is really a question about precedence.
 
priya kandasamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me know more about it since I am new to Java......
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the link in Campbell's post above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!