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

if( x == null ) return false ;


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


if( x instanceof null ) return false ;


if( x eq null ) return false ;


Author Explanation :

Answer A is the only way to check a reference for the null value. <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/>
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/>
D is invalid Java syntax

© Copyright 1997 - 2019 | | | All Rights Reserved.
Bestdotnet google plus
Enquiry Now