To the best of my knowledge this [Java translates x == y into x.equals(y)]
goes against the most basic primitive/reference objects concept. A
comparison between x and y [x == y] would compare the 'pointers' x and y to
see if they point to the same object, whereas x.equals(y) compares the
values of the x and y objects for equality (they may have the same value,
but not necessarily be the same object).
If x and y pointed to the same object, x == y and x.equals(y) would both
yield true;
However, if x and y had equal values, BUT weren't the same object, x == y
would yield false and x.equals(y) would yield true.
e.g.
String x = new String("My String");
String y = x;
if (x == y) { // condition is true and this code will be executed }
if (x.equals(y)) { // condition is true and this code will be executed }
y = new String("My String");
if (x == y) { // condition is false and this code will not be executed.
// y was assigned to a new string object.
// x and y no longer point to the same object. }
if (x.equals(y)) { // condition is true and this code will be executed.
// The VALUE of x and y is still equal. }
Please feel free to correct me on this.
Barry Scott
IJava UK
----- Original Message -----
From: Richard Clark <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, January 06, 2000 12:56 AM
Subject: Re: Checking for null
> Look at my code again...
>
> if (null == s || s.length() == 0) ...
>
> Java translates x == y into x.equals(y). If I had said
> if (s == null)
> and s was null, you would get a null pointer exception. However, the
> 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.
>
> 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.
>
> By the way, you could also say:
> if (null != s && s.length() != 0)
> which would yield the same result as Mark'code below.
>
> ...Richard
>
>
> -----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
>
>
___________________________________________________________________________
> 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
>
___________________________________________________________________________
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