File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short Circuit AND

 
balaguru
Greenhorn
Posts: 4
  • 0
  • 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");
}
}
}
 
Aqeel Ahmad
Greenhorn
Posts: 11
  • 0
  • 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
  • 0
  • 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.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'balaguru'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
Graeme Brown
Ranch Hand
Posts: 193
  • 0
  • 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");
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic