23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.6.2.1 Utility methods<br />

<strong>Java</strong> I/O<br />

For completeness, I'll note that DecimalFormatSymbols overrides the usual three utility<br />

methods from java.lang.Object, hashCode(), clone(), and equals():<br />

public int hashCode()<br />

public boolean equals(Object obj)<br />

public Object clone()<br />

One DecimalFormatSymbols object is equal to another DecimalFormatSymbols object if<br />

they're instances of the same class and all their symbols are the same.<br />

16.6.3 Constructing Decimal Formats with Patterns and Symbols<br />

Most of the time, you use the factory methods in NumberFormat to get DecimalFormat<br />

instances. However, there are three public DecimalFormat constructors you can use to create<br />

DecimalFormat instances directly:<br />

public DecimalFormat()<br />

public DecimalFormat(String pattern)<br />

public DecimalFormat(String pattern, DecimalFormatSymbols symbols)<br />

The no-argument constructor creates a decimal format that uses the default pattern and<br />

symbols for the default locale. The second constructor creates a decimal format that uses the<br />

specified pattern and the default symbols for the default locale. The third constructor creates a<br />

decimal format that uses the specified pattern and the specified symbols for the default locale.<br />

These are useful for special cases that aren't handled by the default patterns and symbols.<br />

16.7 An Exponential Number Format<br />

The DecimalFormat class is useful for medium-sized numbers, but it doesn't work very well<br />

for exceptionally large numbers like Avogadro's number<br />

(6,022,094,300,000,000,000,000,000) or exceptionally small numbers like Planck's constant<br />

(0.00000000000000000000000000625 erg-seconds). These are traditionally written in<br />

scientific notation as a decimal number times 10 to a certain power, positive or negative; for<br />

example, 6.0220943 × 10 23 and 6.25 × 10 -27 erg-seconds. In most programming languages,<br />

including <strong>Java</strong>, an E followed by either a + or a - is used to represent "× 10 to the power"; for<br />

example, 6.0220943E+23 or 6.25E-27 erg-seconds.<br />

The java.text package does not provide support for formatting numbers in scientific<br />

notation, [3] so as the final example of this chapter, I'll develop a new subclass of<br />

NumberFormat that does use scientific notation. Technically, scientific notation requires<br />

exactly one nonzero digit before the decimal point, but I'll be a little more general than that,<br />

providing for numbers like 13.2E-8 as well.<br />

The NumberFormat class is abstract. It declares three abstract methods any subclass must<br />

implement:<br />

3 The java.lang.Double class's toString() methods do format numbers less than 0.001 or greater than 10 million in scientific<br />

notation.<br />

423

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!