Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > "stringObj == null" vs "stringObj.equals(null)", for null check??

Reply
Thread Tools

"stringObj == null" vs "stringObj.equals(null)", for null check??

 
 
qazmlp1209@rediffmail.com
Guest
Posts: n/a
 
      03-29-2006
As I am aware, it is required to use equals() method(and not '==') for
equality comparision of two String objects.
But, what exactly is recommended for 'null' check?
if( stringObj == null )
or
if( stringObj.equals( null ) )

 
Reply With Quote
 
 
 
 
Timbo
Guest
Posts: n/a
 
      03-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> As I am aware, it is required to use equals() method(and not '==') for
> equality comparision of two String objects.
> But, what exactly is recommended for 'null' check?
> if( stringObj == null )
> or
> if( stringObj.equals( null ) )
>

If stringObj is null, then the second one (using .equals) will
throw a NullPointerException, because you are attempting to
dereference a null pointer. So, the first one should be used.
 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      03-29-2006
(E-Mail Removed) wrote:
> As I am aware, it is required to use equals() method(and not '==') for
> equality comparision of two String objects.
> But, what exactly is recommended for 'null' check?
> if( stringObj == null )
> or
> if( stringObj.equals( null ) )
>


A String object cannot be null, only a String reference can be null.
You are testing if the String reference is null, so ...
Anyway, if stringObj was null the equals() version would throw a NPE...
 
Reply With Quote
 
weironghai@gmail.com
Guest
Posts: n/a
 
      03-29-2006
stringObj == null

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-29-2006
On 29 Mar 2006 01:08:56 -0800, (E-Mail Removed) wrote, quoted
or indirectly quoted someone who said :

>But, what exactly is recommended for 'null' check?
>if( stringObj == null )
>or
>if( stringObj.equals( null ) )


the first is preferred. The second would likely like blow up with a
null pointer exception since people don't always defend against sort
of thing.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-29-2006

"Roedy Green" <(E-Mail Removed) > wrote in
message news(E-Mail Removed)...
> On 29 Mar 2006 01:08:56 -0800, (E-Mail Removed) wrote, quoted
> or indirectly quoted someone who said :
>
>>But, what exactly is recommended for 'null' check?
>>if( stringObj == null )
>>or
>>if( stringObj.equals( null ) )

>
> the first is preferred. The second would likely like blow up with a
> null pointer exception since people don't always defend against sort
> of thing.


That is, the second one will always NPE is stringObj is null, and possibly
even if it isn't.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
NULL macro vs. 0 as null pointer? Ken C++ 69 07-31-2004 04:45 AM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments