6 Replies Latest reply: Feb 13, 2004 12:26 PM by 807582 RSS

    I am confused i.toString==i.toString

    807582
      Hello,
      I was writing programs to get an understanding of the java certificate exam.
      I am not sure this is related but can anyone explain me why the follwing is happening?
      It is late and my brain is pickled inside my head.
      --------------first one-----jdk1.3.1 (Jbuilder 7)

      public class test {

      public test() {
      }
      public static void main(String[] args) {
      String[] aas = { "array", "of", "String", };
      test test1 = new test();
      Integer i=new Integer(12);
      System.out.println("i==i:"+(i.toString()==i.toString()));
      Integer i1=new Integer(10);
      Integer i2=new Integer("10");
      System.out.println("i1==i2?:"+(i1.toString()==i2.toString()));
      System.out.println("i1==i1?:"+(i1.toString()==i1.toString()));
      Integer i3=new Integer(12);
      System.out.println("i==i:"+(i3.toString()==i3.toString()));
      }
      }
      --------------------------Output of this program
      D:\JB7\jdk1.3.1\bin\javaw -classpath............ test
      i==i:false
      i1==i2?:false
      i1==i1?:false
      i==i:false
      ------------------------------------------------------------------------------------
      -----second program------jdk1.4.2-------------------------
      public class str{
      public static void main(String args[]){
      Integer i1=new Integer(10);
      Integer i2=new Integer("10");
      System.out.println("i1==i2?:"+(i1.toString()==i2.toString()));
      System.out.println("i1==i1?:"+(i1.toString()==i1.toString()));
      Integer i3=new Integer(12);
      System.out.println("i==i:"+(i3.toString()==i3.toString()));
      }
      }
      ---------------------------output:----------------------
      C:\proj\cert>java str
      i1==i2?:true
      i1==i1?:true
      i==i:false
        • 1. Re: I am confused i.toString==i.toString
          807582
          It's simple. When == is used to compare object references, it will only tell you whether the two references point to the same object or not. It is possible for two independent Strings to represent the same sequence of characters. In this case, == will return false. Although they are conceptually the same string of characters, they are different objects.
          • 2. Re: I am confused i.toString==i.toString
            807582
            my problem was about the differences between outputs.
            jdk1.3.1 i.toString()==i.toString() is false
            but in
            jdk1.4.2 i.toString()==i.toString() is true.
            is this a bug or a memory management difference?
            what is the answer for
            i.toString()==i.toString()
            is it always false? so why in jdk1.4.2 it is true?
            king regards
            baris
            • 3. Re: I am confused i.toString==i.toString
              800560
              I get false for JDK 1.4.1. My guess would be that the str class that you ran was not the str class that you thought you were running.
              • 4. Re: I am confused i.toString==i.toString
                807582
                all right i tried it again
                public class str in both jdk1.4.2 and jdk1.3.1
                here are the results
                ------------------------------------------output1---------------------------------------------------------------------------------------
                C:\proj\str>notepad str.java

                C:\proj\str>javac str.java

                C:\proj\str>java str
                i1==i2?:true
                i1==i1?:true
                i==i:false

                C:\proj\str>java -version
                java version "1.4.2_02"
                Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
                Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

                C:\proj\str>
                ----------------------------------------Output2-------------------------------------------------------------------------
                D:\JB7\jdk1.3.1\bin\javaw -classpath "C:\Documents and Settings\Administrator\jbproject\test\classes;D:\JB7\lib\xerces.jar;D:\JB7\jdk1.3.1\demo\jfc\Java2D\Java2Demo.jar;D:\JB7\jdk1.3.1\jre\lib\i18n.jar;D:\JB7\jdk1.3.1\jre\lib\jaws.jar;D:\JB7\jdk1.3.1\jre\lib\rt.jar;D:\JB7\jdk1.3.1\jre\lib\sunrsasign.jar;D:\JB7\jdk1.3.1\lib\dt.jar;D:\JB7\jdk1.3.1\lib\htmlconverter.jar;D:\JB7\jdk1.3.1\lib\tools.jar" str
                i1==i2?:false

                i1==i1?:false

                i==i:false
                -----------------------------------------------------------------------------------------------------------------------
                no change...... results are different.
                • 5. Re: I am confused i.toString==i.toString
                  807582
                  Hi,

                  I would expect this behaviour. The toString() method simply returns a String (or, more accurately, a String object). Two String objects may well have the same value, but == will return false, because it only tests object identity, it does not test the 'value' of an object.

                  It would appear that in one version of the jdk Integer.toString() creates only a single String object for each Integer object and returns that object for each subsequent toString() call, and that in another version it creates a new String object for every call to toString(). Both are acceptable ways of implementing toString(). Your test is simply highlighting an implementation detail.

                  You should really be using the equals() method, i.e.
                  i.toString().equals( i.toString() )
                  if you actually want to compare the Strings, as opposed to the String objects.

                  If you really want to use object equality then you will need to use the String.intern() method:
                  i.toString().intern() == i.toString().intern()
                  which makes guarantees about String objects. Both of these comparisons should always return true.

                  Ol.
                  • 6. Re: I am confused i.toString==i.toString
                    807582
                    May not be helpful but I see a comma at the end
                    of your last string in the string array declaration.
                    I hope it doesn't cost you more headaches since
                    such small syntax errors are difficult to find..