• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

and

 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, by playing the Rules Roundup, it has come to my attention that I can just as correctly write

if( x<5 & y>3)

as

if( x<5 && y>3)

so why have I, up until this point, always written &&, and always seen it written as &&?
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is a "short-circuit" boolean operation. So if the first (left-to-right) expression evaluates false, the second expression is not evaluated since false and true evaluates false.

evaluates both expressions regardless. This is significant if your code depends on the second expression being evaluated or not.
 
Saloon Keeper
Posts: 26020
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if ( (1 > 2) && deleteAllMyFiles() ) {
System.out.println("This can never happen");
}
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim Holloway:
if ( (1 > 2) && deleteAllMyFiles() ) {
System.out.println("This can never happen");
}



As Edwin explains above, this snippet will not only skip the println() call, but it will also skip deleteAllMyFiles().

Layne
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently found this in code:

if( a != null & a.equals("b") ) ...

which still gets a null pointer exception when a is null. They intended a shortcut && in there to make that work. Being a PITA I changed it to:

if ( "b".equals(a) ) ...

 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic