• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about this question

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found that all && operator can be replaced by & operator in any program, same as || and |. So what's the point to have && and ||?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The && and || short-circuit (evaluation stops once the value of the entire expression can be determined). When you want full evaluation of a boolean expression, you can use & and |.
Junilu
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by david hu:
I found that all && operator can be replaced by & operator in any program, same as || and |. So what's the point to have && and ||?


example....
if ( some_method1() || some_method2() ) {
...
...
}
So if some_method1() return true then some_method2() will be short-circuited which means it won't get to run. But what if some_method2() sets somekind of flags on some static var that u going to test later?(not a good idea but it happens)
Then you have to use | so there wouldn't be short-circuit going on and both methods get to run. Same with &&.
On the other hand, what if you don't really need some_method2() to run if some_method1() return true?? Why do more work if you dont need to. At least the program run a bit faster and its the smart to handle it.
A lot of compiler tries to compile your code that way.
happy new year.

[This message has been edited by FEI NG (edited January 03, 2002).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Short circuit logical operators come in handy when you want to handle incoming parameters in a safe way. It will allow us to use a single if statement where we might have to use more than one. Play around with the following code to see what I mean.

NOTE: Inside the safeUsage method we could have done the following:

but using short circuit logical operators we have accomplished it with less typing and much cleaner code.
Regards,
Manfred.
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great example! useful!
 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for all your help!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic