Actually, you can use == to compare strings but only if the strings are
created by; String x = ""; and not by; String x = new String().

It has to do with the where java stores the string object (literal pool
vs programmers space).

But either way, you should always use the equals method to compare two
object as long as the overriding equals method your using compares what
you want it to compare.

Alan Sparago
[EMAIL PROTECTED]
602 494 9596


-----Original Message-----
From: Jacob Kjome [mailto:hoju@;visi.com] 
Sent: Thursday, October 17, 2002 11:06 AM
To: Tomcat Users List
Subject: Re: expression ALWAYS evaluates to "if"... NEVER to "else"

Hello Z.BEAT,

If these are both java String objects then all you are comparing is if
the memory location of string object #1 is the same as that of String
object #2.  This is probably not true.  You need to compare like this.

if (paramPassword.equals(secretCode)) {

} else {

}

You can only count on the "==" operator to compare an object to null
or to compare simple data types such as int, boolean, long, etc....

Jake

Thursday, October 17, 2002, 12:57:35 PM, you wrote:

ZB> In the following code snippet, the expression ALWAYS
ZB> evaluates to the "if" statement block:

ZB> String paramPassword =
ZB> request.getParameter("paramPassword");
ZB> String secretCode = "secret";

ZB> if(paramPassword != secretCode)
ZB> {

ZB> }
ZB> else
ZB> {

ZB> }

ZB> However, my debugging flags that I send in an HTML
ZB> comment CLEARLY show that the two variables have the
ZB> same value:

ZB> <!-- DEBUG FLAGS
ZB> paramPassword: secret
ZB>    secretCode: secret
-->>

ZB> What is going on?   Am I missing something obvious?

ZB> Thanks!

ZB> __________________________________________________
ZB> Do you Yahoo!?
ZB> Faith Hill - Exclusive Performances, Videos & More
ZB> http://faith.yahoo.com

ZB> --
ZB> To unsubscribe, e-mail:
<mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
ZB> For additional commands, e-mail:
<mailto:tomcat-user-help@;jakarta.apache.org>



-- 
Best regards,
 Jacob                            mailto:hoju@;visi.com


--
To unsubscribe, e-mail:
<mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail:
<mailto:tomcat-user-help@;jakarta.apache.org>





--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>

Reply via email to