Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JXAM question on string and logical operators

 
megha gupta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic