File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes short circuit operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "short circuit operators" Watch "short circuit operators" New topic
Author

short circuit operators

ketki kalkar
Ranch Hand

Joined: May 09, 2007
Posts: 36
1.public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t && ((i++) == 0));
b = (f && ((i+=2) > 0));
System.out.println(i);
}
}
answer is 1.

2.public static ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
}
}


0
1
2
3
answer is 3.
Please explain?




0
1
2
3
suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
first answer

initially i=0;
in the sixth line it is incremented

so the final answer is 1....

second answer

same logic
i=0
6th line it is incremented by 1...so i=1
in the next line it is incremented by 2...so i=i+2>>>i=1+2

answer is 3


SCJP aspirant<br />SCWCD aspirant
ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
1.1.public static ShortCkt {
2.public static void main(String args[]) {
3.int i = 0;
4.boolean t = true;
5.boolean f = false, b;
6.b = (t && ((i++) == 0));
7.b = (f && ((i+=2) > 0));
8.System.out.println(i);
}
}
answer is 1.

2.1.public static ShortCkt {
2.public static void main(String args[]) {
3.int i = 0;
4.boolean t = true;
5.boolean f = false, b;
6.b = (t & ((i++) == 0));
7.b = (f & ((i+=2) > 0));
8.System.out.println(i);
}
}

Code 1 uses short circuit operator "&&". This evaluates the second part of the expression only if the first part is true. If the first part is false, the second part of the expression is ignored.
In line 6, in the first part, t is true. Hence, the second part is evaluated and i is incremented to 1.In line 7, first part, f is false so, the second half of the expression is ignored. Hence, i = 1.

Code 2 uses logical "&" operator. Hence both expressions are evaluated and i = 3.

Hope you are clear with the answer.
suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
Ram...

this code has nothing to do with the short Circuit operators...they are asking the value of i....which you can evaluate just looking at the increment operators....its another way of confusing scjp aspirants....
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by suresh koutam:
this code has nothing to do with the short Circuit operators...they are asking the value of i....which you can evaluate just looking at the increment operators....its another way of confusing scjp aspirants....


If this code has nothing to do with the short circuit operators, then I ask you for the detailed explanation why the outputs are different.
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Suresh,

The output of the first example does depend on the short circuit operation.

This line of code


does not change the value of i due to the += operator being on the right-hand side of the short circuit && operator when the left-hand side is false.
suresh koutam
Ranch Hand

Joined: Dec 29, 2004
Posts: 30
sorry guys..i was little excited seeing the answers...i agree with you guys..
thanks for correcting...


Suresh Koutam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: short circuit operators