Hi,

Please stop all these mistakes ;-).

In the "heart" of Java, the == operator has been overloaded (since I
speak French, I'm not sure this is the right term) for a few classes of
the java.lang package. String, like Integer, Double and other wrapping
classes, belongs to these classes. Though, the == operator and the
equals method do the same thing : it compares the content of the String
instance.

Sample :

String str1="Hello";
String str2="Hello";
if(str1==str2) {
        System.out.println("str1==str2");
}
if(str1.equals(str2)) {
        System.out.println("str1.equals(str2))");
}

The two "if conditions" are true although the two String objects are
located into two different memory addresses.

I hope this clarifies the way String works. Be careful, it's not the
same for StringBuffer. For StringBuffer, Bruce, Vance, Craig and others
are right.

--
Denis Jaccard ([EMAIL PROTECTED])
Java Developer & Trainer - Java Certified Programmer

-------
Open your windows to Java
-------

Adenia Software
Tel. : +41 (0)32 724 96 57
http://www.adenia.com



Chris Mcgarel wrote:
>
> Can someone expalin to a novice why I must use:
>          if (Request.Form("myFormElement").equals("myString"))
> rather than a straight comparison operator:
>         if (Request.Form("myFormElement")=="myString")
> ?
>
> The latter does not work for me even though both left and right sides of the
> statement are Strings.
>
> The full code for form.jsp is:
>
> <body>ody bgcolor=darkblue text=white>
> <font face=arial>
> <%
>     String myTest = "";
>
>     myTest = Request.Form("mySubmit");
>
>     if (myTest=="Click")                        // doesn't work, must be if
> (myTest.equals("Click"))
>     {
>         out.println ("CLICKED!!!");
>     }
>
> %>
>
> <form action="form.jsp">
> <input type=submit name="mySubmit" value="Click">
> </form>
>
> </font>
> </body>
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to