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)");

Milt Epstein
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

Reply via email to