Here are the proposed alternatives to be used in a method to return false if the object reference variable 'x' has the null value. Which statement will work?

Back to Questions
A)

if( x == null ) return false ;

B)

if( x.equals( null ) ) return false ;

C)

if( x instanceof null ) return false ;

D)

if( x eq null ) return false ;

 

Author Explanation :

Answer A is the only way to check a reference for the null value. <br/>
<br/>
Answer B is incorrect because if x is null, there will not be an object whose equals method can be called. This statement would cause a NullPointerException.<br/>
<br/>
Answer C is incorrect because only a reference type, such as a class, interface, or array, can be the right operand of the instanceof operator. <br/>
<br/>
D is invalid Java syntax


© Copyright 1997 - 2019 | www.bestdotnettraining.com | www.deccansoft.com | All Rights Reserved.
 
Bestdotnet google plus
Enquiry Now



Input symbols