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: Stephen C. Upton
Subject: Re: Differences between JDK and Kaffe (Was: Swarm, Kaffe and AWT)
Date: Thu, 27 Sep 2001 12:37:10 -0400

Tom,

I'm not sure what's going on either, and I get true for all of
statements in your original code. However, if I modify the code as
below, then I get false for the 2nd and 3rd lines, like you'd (or I'd)
expect. In the jdk documentation it mentions something about Strings
being mutable and that they can be shared. ("Strings are constant; their
values cannot be changed after they are created. String buffers support
mutable strings.
Because String objects are immutable they can be shared.") Haven't
checked further - wiser computer dudes will have to answer this one!

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

steve

Tom Wainwright wrote:
> 
> 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.


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