• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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");
}
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!