On Wed, 5 Jan 2000, Richard Clark wrote:

> Look at my code again...
>
>   if (null == s || s.length() == 0) ...

You probably should've stopped there, as the above is correct and will
do what the original poster wanted.  Much of what you say below is
incorrect.


> Java translates x == y into x.equals(y). If I had said

This is not true.  "x == y" is very different than "x.equals(y)".  The
first one checks for identity of objects (i.e. the two variables refer
to the same thing), and the latter checks for "equals" as defined by
the particular object involved.  Java's Object class has an equals
method that will be used if it it not overriden (and I believe it uses
the object identity check), but many objects (e.g. String) do override
it.  null does have to be handled as a special case with x == y, but I
don't know the specifics.

>   if (s == null)
> and s was null, you would get a null pointer exception. However, the

No you wouldn't.  "s == null" and "null == s" are equivalent.  (I had
figured you wrote it that way because of the convention some C
programmers use of putting the constant/special value first in the
equality check so that the compiler will notice it if you accidentally
use one '=' instead of two, and mistakenly do an assignment.)

> constant null is handled specially -- it has an equals method that
> always works. Thus, saying
>   if (null == s)
> will not fail with a null pointer exception.

As I state above, neither will "if (s == null)".


> I also used the short-circuiting "or" operator || to connect the two
> pieces.  In effect, a || b means "if a evaluates to false, evaluate
> b."
>
> In a case like this, || often leads to cleaner and more efficient
> code than nesting "if" statements.

These are both correct, and explains why your code will not cause a
NPE if s is null.


> By the way, you could also say:
>   if (null != s && s.length() != 0)
> which would yield the same result as Mark'code below.

This is just that old logical rule (whose name I forget) that says
that:

  ~(A rel B || C rel D) == ~A ~rel ~B && ~C ~rel ~D
and
  ~(A rel B && C rel D) == ~A ~rel ~B || ~C ~rel ~D

where "~" is negation, "rel" is any relation (and I abuse the syntax a
bit to use "~rel" to indicate the inverse operation of "rel").

I should add that once again a totally off-topic question (and one
that could be answered with a very basic Java book) causes much
traffic on the list.  What's even more disturbing, perhaps, is that
many of the resoponses were partially or completely inaccurate.


> -----Original Message-----
> From: Mark Foley [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 05, 2000 4:22 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [SERVLET-INTEREST] Checking for null
>
>
> Hi,
>
> Wouldn't your conditional statement cause a null pointer exception if s is
> null? Try it!
>
> I do this:
> ...
> if(null != s) {
>     if(s.length() != 0) {
>         // it's not null and it's not empty
>     }
> }
>
> Regards,
> Mark

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to