5 Replies Latest reply: Feb 8, 2007 10:19 PM by 807599 RSS

    string and string buffer

    807599
      Hi

      String s1 = "Java";
           StringBuffer s2 = new StringBuffer(s1);
           System.out.print(s2.equals("Java") + "," + s1.equals(s2));

      i am getting false ,false as out put.
      As of my knowledge equals() method compares the contents of string object ,is that is true why i am getting false as output.


      When i have replaced StringBuffer with the String i am getting as true, true.

      can any body tell me what is exact difference between string and stringbuffer.

      Thanks in Advance
        • 1. Re: string and string buffer
          807599
          Hi

          String s1 = "Java";
          uffer s2 = new StringBuffer(s1);
          System.out.print(s2.equals("Java") + "," +
          "," + s1.equals(s2));

          i am getting false ,false as out put.
          f my knowledge equals() method compares the contents
          of string object ,is that is true why i am getting
          false as output.


          When i have replaced StringBuffer with the String i
          am getting as true, true.

          can any body tell me what is exact difference between
          string and stringbuffer.

          Thanks in Advance
          StringBuffer and String cannot be compare using equals method
          you should use 'toString' method for StringBuffer object,

          for example
          String s1="Java"
          StringBuffer S2 = new StringBuffer(s1);
          
          System.out.prinlnt(s2.toString().equals("Java") + "," + s1.equals("s2.toString());
          • 2. Re: string and string buffer
            807599
            hi p_epi

            Thanks for the reply.

            can u tell me the reason why we should not compare the string and stringbuffer?
            I am sorry if this is very basic question.

            Thanks in Advance
            • 3. Re: string and string buffer
              807599
              hi p_epi

              Thanks for the reply.

              can u tell me the reason why we should not compare
              the string and stringbuffer?
              I am sorry if this is very basic question.

              Thanks in Advance
              The simplest answer is because the author's didn't provide an equals() implementation that can compare the two.

              Does that suffice or are you looking for the reasoning behind the author's not doing so?
              • 4. Re: string and string buffer
                807599
                From the API:
                Compares this string to the specified object. The result 
                is true if and only if the argument is not null and is a String
                object
                that represents the same sequence of characters
                as this object.
                Since the object you are comparing the String to is not another String object it must return false.

                Stoopid side-scroller

                Message was edited by:
                flounder
                • 5. Re: string and string buffer
                  807599
                  The String method contentEquals(java.lang.StringBuffer) should work here too, or contentEquals(java.lang.CharSequence) more generally.