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