Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4095026

Changing a StringBuffer can change an existing String

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P1 P1
    • None
    • 1.2.0
    • core-libs
    • x86
    • windows_nt



      Name: bb33257 Date: 11/24/97


      In the latest 1.2 source workspace that we (Taligent) have, there
      is a serious problem in String and StringBuffer. If you run the
      following code:

      StringBuffer b = new StringBuffer("foo");
      String s1 = b.toString();
      b.setLength(0);
      b.append("bar");
      String s2 = b.toString();
      System.out.println("S1: "+s1+", S2: "+s2);

      it will print "S1: bar, S2: bar" rather than "S1: foo, S2: bar".

      It appears that there's some overzealous buffer sharing going on
      between String and StringBuffer. If String and StringBuffer are
      sharing the same buffer, they need to use a copy-on-write
      implementation to make sure that a String can never be changed
      once it's constructed.

      This problem is blocking us from getting our remaining I18N bug
      fixes integrated into 1.2.
      ======================================================================

            dviswanasunw Deepa Viswanathan (Inactive)
            bcbeck Brian Beck (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: