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

[Fmt-Nu] Doc: DecimalFormat.getDecimalFormatSymbols() returns clone, undocumented

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 6
    • core-libs
    • b30
    • x86
    • windows_nt



      Name: rmT116609 Date: 05/15/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :

      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      The javadoc for method java.text.DecimalFormat.getDecimalFormatSymbols() states:

      "Returns the decimal format symbols, which is generally not changed by the programmer or user."

      However, the source code reads:

        public DecimalFormatSymbols getDecimalFormatSymbols() {
          try {
            // don't allow multiple references
            return (DecimalFormatSymbols) symbols.clone();
          } catch (Exception foo) {
            return null; // should never happen
          }
        }

      In other words, the user of the function does not get a reference to the DecimalFormatSymbols used in the DecimalFormat's "symbols" field, but a reference to a copy.

      The documentation should clearly state this, or the function should return a reference to the "symbols" field.

      Such behaviour makes (possibly reasonable) code such as this pointless:

        DecimalFormat f = getTheFormat();
        DecimalFormatSymbols s = f.getDecimalFormatSymbols();
        s.setDecimalSeparator(',');

      One wonders what the advantage of forcing the programmer to adopt this rather longwinded workaround is:

        DecimalFormat f = getTheFormat();
        DecimalFormatSymbols s = f.getDecimalFormatSymbols();
        s.setDecimalSeparator(',');
        f.setDecimalFormatSymbols(s);

      However, my real gripe is about the time I've lost trying to understand why my changes to the DecimalFormat weren't working. A developer without source-code at hand would not find the necessary information (that a clone() call is
      involved) in the documentation.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.text.DecimalFormat;
      import java.text.DecimalFormatSymbols;

      public class A {
        public static void main(final String args[]) {
          DecimalFormat f = new DecimalFormat();
          f.getDecimalFormatSymbols().setDecimalSeparator('X');
          char separator = f.getDecimalFormatSymbols().getDecimalSeparator();
          System.out.println(separator);
        }
      }
      ---------- END SOURCE ----------
      (Review ID: 146600)
      ======================================================================

            peytoia Yuka Kamiya (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: