aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50

Which code fragments cause an object 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))
Could someone please explain me why the answers here are A & C whereas in the following
Which code fragments cause an object 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)) the answer is A, B, C, D.
Please explain me how it works in these codes.
Thanks in advance
Mita

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Mita,
This question deals with the difference between short-circuit logical operators and bitwise operators in the way they evaluate expressions.
For an && expression, if the first operand evaluates to false, the second operand is never evaluated.
Similarly for an | | expression, if the first operand evaluates to true, the second operand is never evaluated.
Unlike the logical short-circuit operators, the bitwise operators & and | always evaluate both the operands and hence can cause NullPointerException.

Hope this helps,
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
Thank you so much Ajith.That sure helped a lot.
Mita
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullPointerException