Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Short Circuit AND  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shoudn't be print first statment as tstate is true no matter the right hand operand, but it prints second. pls clarify;
class ShortDemo{
public static void main(String []arg){
String season;
boolean tstate=true;
boolean fstate=false;
if(tstate&&fstate){
System.out.println("true shortckt and false is true");
}else {
System.out.println("true shortckt and false is false");
}
}
}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember:
(true && false) is false
(true && true) is true
(false && false) is false
(false && true) is false
 
balaguru
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, clear now. i thought, this truth table applied only for AND operation.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'balaguru'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between '&' and '&&' is that with '&' all args are evaluated, however with '&&' the right hand arg is only evaluated if the left arg evaluates to 'true'.
Hence in the following code the first 'if' compiles OK, but the second is rejected because s has not been initialized.
This technique can be useful for avoiding null pointer exceptions at runtime.
public class Test {
public static void main(String args[]) {
String s;
if (false && s.equals("abc"))
System.out.println("First");
if (false & s.equals("abc"))
System.out.println("Second");
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!