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

Discussion in 'Java' started by qazmlp1209@rediffmail.com, Mar 29, 2006.

  1. Guest

    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 ) )
    , Mar 29, 2006
    #1
    1. Advertising

  2. Timbo Guest

    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.
    Timbo, Mar 29, 2006
    #2
    1. Advertising

  3. 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...
    Thomas Schodt, Mar 29, 2006
    #3
  4. Guest

    stringObj == null
    , Mar 29, 2006
    #4
  5. Roedy Green Guest

    On 29 Mar 2006 01:08:56 -0800, 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.
    Roedy Green, Mar 29, 2006
    #5
  6. "Roedy Green" <> wrote in
    message news:eek:...
    > On 29 Mar 2006 01:08:56 -0800, 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.
    Mike Schilling, Mar 29, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    1
    Views:
    687
    Michael D. Ober
    Jun 14, 2004
  2. mit
    Replies:
    1
    Views:
    792
    Ramu Pulipati
    Jan 25, 2006
  3. kris
    Replies:
    0
    Views:
    504
  4. G Fernandes
    Replies:
    9
    Views:
    577
    DHOLLINGSWORTH2
    Feb 27, 2005
  5. Mike
    Replies:
    12
    Views:
    703
    Daniel Pitts
    Sep 26, 2008
Loading...

Share This Page