• 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

&& Operator

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt the given answer and explanation. Please let me know whether i am right.

Question
Given the following definition:

String s = null;

Which code fragment will cause an exception of type NullPointerException to be thrown.
a.if ((s != null) & (s.length()>0))
b.if ((s != null) && (s.length()>0))
c.if ((s != null) | (s.length()>0))
d.if ((s != null) || (s.length()>0))

i thought a, c, d will throw NPE but not b. But the given answer is a,b,c,d
explanation provided as

This question revolves around the short-circuit logical operators (at least some of it does!).

For the following, LHS is left-hand side, RHS = right hand side.

Answer 1 must be fully evaluated. The LHS is true and so the RHS needs to be evaluated to ensure the entire expression is true. This is an AND operation, and if the LHS is true, then the RHS must be evaluated for the expression to be true.

Answer 2 is the same as above. Both sides need to be evaluated for an AND expression to be true (if the LHS is true, that is)..

>>>>

Let me know whether answer 2 is right still
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. 'B' should not throw a NullPointerException.

In B, the LHS evaluates to false. Because this is testing for an "and" condition (in which both sides must be true) and this is the short-circuiting version (&& rather than &), the RHS will not be evaluated.


[ December 15, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct.
no chance of getting he exeception in the line b
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At any cause doesnt s.length() throw an exception for null strings..
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balaji Sampath:
At any cause doesnt s.length() throw an exception for null strings..


Not when it's on the right-hand side of a short-circuiting "and" comparison (&&) in which the left-hand side is false. In that situation, the left side evaluating to false is enough to know that the comparision will be false regardless of how the right side evaluates. Therefore, the test "short-circuits" and does not bother to evaluate the right side.
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey marc,
I agree with you! But can you explain why LHS fails for the above choice bcoz if you declare :
String test= null;
if(test!=null){
System.out.println("Null check passed");
}
It wouldnt throw any exceptions ..
So in that case dont all the choices quoted should have the LHS condition passed isnt..

Not sure need answer..

Regards
Balaji.S
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jack Ryan:
String test= null;
if(test!=null){
System.out.println("Null check passed");
}


What do you mean by Pass here.. Not getting a NPE

In the question posted above,
s!=null is false so the RHS is not evaluated. Its the RHS that has the potential of throwing NPE, not the LHS.
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not been brief:
It wouldn't throw any exceptions !!..
Since (s!= null) does not throw exceptions wont the LHS of the choices be true..
Thereby while evaluvating the RHS, (s.length()) would throw excepton in all the choice listed:
a.if ((s != null) & (s.length()>0))
b.if ((s != null) && (s.length()>0))
c.if ((s != null) | (s.length()>0))
d.if ((s != null) || (s.length()>0))

So answer for the question posted would be ABCD.

Again im a novice!!Not a expert..

Thanks
Balaji.S
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not throwing an exception does not necessarily mean the expression will evaluate to true. In this case, since s is null, then the expression (s != null) will be false.
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guyzz sorry for misinterpreting the question. I was looking at the question has which segment throws the exception rather thaneach choice has a whole.. Completely understood..

Thanks again
Balaji.s
 
reply
    Bookmark Topic Watch Topic
  • New Topic