swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Differences between JDK and Kaffe (Was: Swarm, Kaffe and AWT)


From: Ken Cline
Subject: Re: Differences between JDK and Kaffe (Was: Swarm, Kaffe and AWT)
Date: Sun, 7 Oct 2001 18:30:31 -0400

> On Fri, 28 Sep 2001, Marie-Edith Bissey wrote:
> > 
> > On Thu, 27 Sep 2001, Tom Wainwright wrote:
> > 
> > [...] By definition, the "==" operator on objects determines if the right 
> > and
> > left hand sides of the expression refer to exactly the same object; [...]
> > To test equality of the contents of objects (rather than their storage 
> > location)
> > use the "equals()" method.
> > 
> > I understand. I also found within the String class the compareTo(String) 
> > method,
> > which deals with the comparison of one string with another (or another 
> > object
> > containing a String).

Another (frequently overlooked) alternative for comparing String objects is to 
use
the String.intern() method.  From the javadoc:
   "...It follows that for any two strings s and t, s.intern() == t.intern() is 
true
    if and only if s.equals(t) is true. ..."

The merits of using String.intern() vs. String.equals() is probably not an 
appropriate
subject for the swarm-support list.  However, I will say that (imho) the choice 
would
be an issue of performance and would depend not only on the implementations of
the javac and the jvm but also on the contents of a typical string.  For 
example, if
the strings being compared typically have different lengths, or start with 
different
characters, then the equals() method would possibly be faster.

Finally, the snippet below (taken from the Java Language Spec*) may shed some
light on when/why reference comparisons, ie '==', evaluate to true:
   "... - Literal strings within the same class in the same package represent 
references
             to the same String object.
        - Literal strings within different classes in the same package 
represent references
             to the same String object.
        - Literal strings within different classes in different packages 
likewise represent
             references to the same String object.
        - Strings computed by constant expressions are computed at compile time 
and
             then treated as if they were literals.
        - Strings computed at run time are newly created and therefore distinct.
        - The result of explicitly interning a computed string is the same 
string as any
             pre-existing literal string with the same contents. ..."

Hope that helps.
Ken.
______________________
* See 
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#19369
   for example code that illustrates the statements above.



                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]