Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Short Circuit AND

 
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");
}
}
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic