This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Short Circuit AND Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Short Circuit AND" Watch "Short Circuit AND" New topic
Author

Short Circuit AND

balaguru
Greenhorn

Joined: Oct 19, 2000
Posts: 4
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

Joined: Sep 23, 2000
Posts: 11
remember:
(true && false) is false
(true && true) is true
(false && false) is false
(false && true) is false


Aqeel
balaguru
Greenhorn

Joined: Oct 19, 2000
Posts: 4
Thank you, clear now. i thought, this truth table applied only for AND operation.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
'balaguru'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
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.
 
subject: Short Circuit AND
 
Similar Threads
Question about & and |
javaprepare question
short circuit logical operands
What this code does
short circuit operators