| Author |
logic behind the shortcut operators
|
Dror Astricher
Ranch Hand
Joined: May 20, 2005
Posts: 31
|
|
hi guys i know that the shortcut operators are || and && and i know why. i was wondering about the logic that | and & not are not shortcut operators as well? (a | b) - when a is true, it doesn't metter what b is and it will always be true. (a & b) - when a is false, it doesn't metter what b is and it will always be false. can someone explaine me the logic behind it? thanks dror
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
|
they're not shortcut operators but mathematical operators.
|
42
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Well, consider what happens if a nd b are replaced with methods a() and b() (both of which return booleans). If I write and if a() returns false - well, obviously the value of c will be false, regardless of what b() returns. However - method b() may have a side effect. As a programmer, I may want to ensure that method b() executes, and has its side effect, regardless of the fact that the value of c may not depend on b() at all. Or, maybe I don't want b() to execute unless it really effectus things. Either one of these situations is possible, really, depending on what the programmer is trying to do. So Java offers a choice - if you want short-circuiting, use && and ||; if you don't want short-circuiting, use & and |. In my experience, there's rarely much reason not to use short circuiting - which is why && and || are much more common (in my experience) than & and |. However the latter are still available as an option, for the (relatively rare) cases where this is what the programmer desires.
|
"I'm not back." - Bill Harding, Twister
|
 |
Dror Astricher
Ranch Hand
Joined: May 20, 2005
Posts: 31
|
|
i know that they are not shortcut operators. but waht is the logic behind it? the logic behind && being a shortcut operator is, that when given (A && B) and A is false, then doesn't metter what will be B and it will always be false. thats why we don't even evaluate B. in (A & B), if A is false, it's also doesn't metter what B will be. what is the logic then not to make it also as a shortcut operator and not to save some operations? thanks dror
|
 |
Dror Astricher
Ranch Hand
Joined: May 20, 2005
Posts: 31
|
|
thanks Jim, i got the logic i wrote my second comment before i got your explanation. wish you all a great day dror
|
 |
Ravi Shankar R
Greenhorn
Joined: Jan 05, 2005
Posts: 7
|
|
I agree with Jim Yingst .... See Using the mathematical operator what is the ouput of the program public class ShortCrkOpr { boolean a(){ return false; } boolean b() throws Exception { throw new Exception(); } public static void main(String[] args) { ShortCrkOpr opr = new ShortCrkOpr(); try { if(opr.a() & opr.b()){ System.out.println("True"); }else{ System.out.println("False"); } if(opr.a() && opr.b()){ System.out.println("True"); }else{ System.out.println("False"); } } catch (Exception e) { System.out.println("Exception caught!!!"); e.printStackTrace(); } } } output.... Exception caught!!! java.lang.Exception ....... so the latter effect of the method b()has come... public class ShortCrkOpr { boolean a(){ return false; } boolean b() throws Exception { throw new Exception(); } public static void main(String[] args) { ShortCrkOpr opr = new ShortCrkOpr(); try { if(opr.a() && opr.b()){ System.out.println("True"); }else{ System.out.println("False"); } if(opr.a() & opr.b()){ System.out.println("True"); }else{ System.out.println("False"); } } catch (Exception e) { System.out.println("Exception caught!!!"); e.printStackTrace(); } } } output is... False Exception caught!!! java.lang.Exception here once the method a() has returned false it wouldn't move to the next method b() to check....
|
 |
 |
|
|
subject: logic behind the shortcut operators
|
|
|