File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JXAM question on string and logical operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JXAM question on string and logical operators" Watch "JXAM question on string and logical operators" New topic
Author

JXAM question on string and logical operators

megha gupta
Greenhorn

Joined: Nov 23, 2000
Posts: 3
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

Joined: Aug 04, 2000
Posts: 103
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

Joined: Nov 23, 2000
Posts: 3
Thanks Kathy,
That's what I thot but was not really sure if my fundas on short circuit operators were clear.
Megha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JXAM question on string and logical operators
 
Similar Threads
JXam Qns
Jxam
Jxam
Jxam
q from Jxam