• 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

Shortcut Operator question on Jxam

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of the questions on the Jxam mock exam, this code was given:
String s = null;
if ((s!=null) && (s.length()>0)) ...
The answer said a null pointer exception would be thrown because the entire expression had to be evaluated. But I thought the whole point of the shortcut operator && was that if the first condition was false ( and here it is because s is null ), you never looked at the second part. Am I missing something here or is the answer which is given incorrect/
Kathy
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy,
I agree with you that the answer might be wrong. I checked your code and it looked fine to me, tried the code in a test class of mine, and it still looked fine!
I think the code should have a single '&' for the answer to be correct. With a singel '&' indeed the whole expression would be evaluated no matter if the string was a null or not.
Rikko
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with Rikko, the entire expression is evaluated only if the operator was '&' and NOT '&&' and therefore making the above code perfectly LEGAL.
 
Kathy Hodgson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both!
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic