But it's quite dangerous to use == to compare strings. You could miss the result
you wnat quite easily.
Test that:
String a = "hello";
String b = "he";
String c = "llo";
String d = b + c;
if (a == d) {
System.out.println("a == b");
} else {
System.out.println("non a == b");
}
if (a.equals(d)) {
System.out.println("a.equals(d)");
} else {
System.out.println("non a.equals(d)");
}
You'll obtain: non a == d BUT a.equals(d).
So, be carefull ... ;-))
Isabelle
Richard Clark <[EMAIL PROTECTED]> on 01/06/2000 12:50:32 PM
Please respond to "A mailing list for discussion about Sun Microsystem's Java
Servlet API Technology." <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Isabelle Bourret/Dallas/US/i2Tech)
Subject: Re: Checking for null
Thanks for the corrections -- somewhere along the line I had picked up the
mistaken notion that Java treated "==" and ".equals" as the same thing
(which always puzzled me as it went against my understanding of languages
that used references.) A quick check of the language specification cleared
that up.
An interesting side note:
String a = "Hello"; String b = "Hello"; // or even String b = "H" + "ello"
if (a == b) System.out.println("a == b");
if (a.equals(b)) System.out.println("a.equals(b)");
prints both "a == b" and "a.equals(b)" using both the Sun JDK 1.2 compiler
and jikes on Windows NT. This is part of what led me astray so long ago, and
I re-ran the experiment today with the same result.
___________________________________________________________________________
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