Milt Epstein wrote:
> On Thu, 6 Jan 2000, Richard Clark wrote:
>
> > 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.
>
> That is interesting. My best guess is that it has to do with how Java
> handles string contants/literals, perhaps "interning" them in such a
> way that they are in a specific memory location; thus a and b above
> would actually be identical. My guess is if you created those strings
> differently, they wouldn't test out as "==" (although of course this
> doesn't say anything about my conjecture). For example, try:
>
> String a1 = "Hello"; String b1 = "Jello";
> # I don't remember the exact syntax of substring, hopefully these are correct
> String a = a1.substring(1); String b = b1.substring(1);
> if (a == b) System.out.println("a == b");
> if (a.equals(b)) System.out.println("a.equals(b)");
>
Sharing constant string expressions (even after they are calculated) is one of the
performance enhancements that was added in JDK 1.2. If you've downloaded the
documentation package, you will see this referenced on page:
$JAVA_HOME/docs/relnotes/features.html
under section 21 ("Performance Enhancements"). A side effect of this change was
that "a == b" would return true in your two scenarios above.
For Java objects, "a == b" is asking whether a and b are actually the same object
or not (it is checking for "identity"), while a.equals(b) utilizes the class's own
notions of what "equality" means to determine whether to return true or false. You
can even override the equals() method in your own classes -- for example, you could
decide to say that two Customer objects are equal as long as they have the same
account number, even though they might be individual instances.
>
> Milt Epstein
Craig McClanahan
>
> Research Programmer
> Software/Systems Development Group
> Computing and Communications Services Office (CCSO)
> University of Illinois at Urbana-Champaign (UIUC)
> [EMAIL PROTECTED]
>
> ___________________________________________________________________________
> 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