Hi,
All my sorry for this bad info and thanks for the clarification.
Good weekend to all.
Denis Jaccard
Ethan Henry wrote:
>
> Denis Jaccard wrote:
> >
> > Please stop all these mistakes ;-).
>
> I'm really sorry, but it's you who is wrong, Denis. I really hope you haven't
> been passing this information to your students.
>
> > 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.
>
> Nope. == always compares object references if applied to a reference type.
> The operation of all operators is defined in the JLS. There's no such
> overloading.
>
> > Sample :
> >
> > String str1="Hello";
> > String str2="Hello";
>
> Note that there will only be one instance of the String "Hello" in the string
> table - Java is smart enough to recycle things if possible.
>
> Try this:
>
> String str3 = new String(str1);
>
> > if(str1==str2) {
> > System.out.println("str1==str2");
> > }
> > if(str1.equals(str2)) {
> > System.out.println("str1.equals(str2))");
> > }
>
> if(str1==str3)
> System.out.println("The same");
> else
> System.out.println("str3 and str1 are different"); // this will run
>
> > The two "if conditions" are true although the two String objects are
> > located into two different memory addresses.
>
> But they're not - that's the flaw in your example.
>
> > 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.
>
> It's exactly the same for StringBuffer...
>
> StringBuffer sb1 = new StringBuffer("Foo");
> StringBuffer sb2 = new StringBuffer("Foo");
>
> sb1 will never be the same as sb2.
>
> == on object references ALWAYS compares the references.
>
> Also, see the JLS, section 15.20.3, available at
> http://java.sun.com/docs/books/jls/html/15.doc.html#236163 -
> it specifically states:
>
> "While == may be used to compare references of type String,
> such an equality test determines whether or not the two operands
> refer to the same String object."
>
> Ethan
> --
> Ethan Henry [EMAIL PROTECTED]
> Java Evangelist, KL Group http://www.klg.com
> "Software Development Productivity"
>
> ===========================================================================
> 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