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: Tom Wainwright
Subject: Re: Differences between JDK and Kaffe (Was: Swarm, Kaffe and AWT)
Date: Thu, 27 Sep 2001 09:06:43 -0700

Marie-Edith Bissey wrote:
> 
> 2) With Kaffe, I could compare a String variable with a string using
> the == operator:
> 
> String variable="first";
> variable=="first"; // return true
> 
> With JDK, the same bit of code returns false. Is there another way
> than == to compare strings?
> 
This is an area of Java where there seems to be some disagreement on
implementation.  By definition, the "==" operator on objects determines
if the right and left hand sides of the expression refer to exactly the
same object; it is not a comparison of identity of object contents.  In
this case, you have created two seemingly identical strings, but they
may be stored in different objects (apparently depending on the compiler
you use), with the result that "first" != "first".  So, the "==" test on
strings is not reliable.  To test equality of the contents of objects
(rather than their storage location) use the "equals()" method.

That said, the test class below returns "true" for all the examples on
my system (Sun's j2sdk1.3.0), which is not what I expected.  Anyone else
have experience with this?

public class EqualityTest {
    static public void main(String[] args) {
        String var = "first";
        String var2 = "first";
        System.out.println(var==var);
        System.out.println(var==var2);
        System.out.println(var=="first");
        System.out.println(var.equals("first"));
        System.out.println("first"=="first");
        System.out.println("first".equals("first"));
    }
}

-- 
Tom Wainwright
NOAA/NMFS/NWFSC
2030 S Marine Science Dr
Newport, OR 97365 USA
address@hidden

                  ==================================
   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]