• 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

JXAM question on string and logical operators

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , I am new here to post a message, but have been visiting javaranch for quite sometime
My query from JXAM:
Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
if ((s != null) & (s.length()>0))
if ((s != null) && (s.length()>0))
if ((s != null) | (s.length()>0))
if ((s != null) | | (s.length()>0))
None of the above.
The JXAM solution says 1, 2, 3, 4 are correct ans, however I feel the solution is wrong.
2 does not throw an exception .
Also I am not clear about the question.
Can someone explain to me please.
Thanks
Megha
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,
I think you're right.
If you set s equal to null, then the first part of all these is false, that is
(s!=null) is false because s DOES equal null.
This question is checking to see if you understand the use of the "short circuit" operators. Sometimes, if you're doing an AND or OR operation on two booleans, all you need to do is check the first value.
If you're doing an AND operation and you check the first operand (the stuff to the left of the &) and that's false - you know that the whole expression is false because both operands must be true for the expression to be true - no point in checking the second operand (the stuff to the right of the &). If the first operand is true, then you need to check the second.
If you're doing an OR operation and you check the first operand and it's true, there's no point in checking the second operand because the expression is true if one or more operands are true. If the first operand is false, you need to go on and check the second operand.
If you just use & or | - both operands are checked.
If you use && or | | (short circuit operators) - the first operand is checked and then the second operand is only checked if it's necessary.
if ((s != null) & (s.length()>0))
Checks first and second operands - s.length throws an exception because s is null.
if ((s != null) && (s.length()>0))
Checks first operand (s!= null) - it's false - so the whole expression must be false so it doesn't bother checking s.length because the short circuit && is used.
if ((s != null) | (s.length()>0))
Checks first and second operands - s.length throws an exception because s is null.
if ((s != null) | | (s.length()>0))
Checks first operand -s!=null is false so has to go on and check the second operand to see if the whole expression is true - s.length throws exception because s is null.
Hope this helps,
Kathy
 
megha gupta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kathy,
That's what I thot but was not really sure if my fundas on short circuit operators were clear.
Megha
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic