go to previous page   go to home page   go to next page highlighting

Answer:

No. Formatting only affects the characters that are generated from the variable, not the value in the variable. In the example, value provides data when format() creates a string of characters, but value itself is not changed.

Wrapper Class Output

import java.text.*;

public class IODemoWrapper
{
  public static void main ( String[] args )
  {
    Integer i = 7654321 ;      // create an Integer thru autoboxing
    Double  d = 11000.0008 ;   // create a  Double  thru autoboxing
    
    DecimalFormat numform = new DecimalFormat(); 
    
    System.out.println( "integer = " + numform.format(i) + "\n double = " + numform.format(d) );
  }
}

A wrapper object may be used as data for format(). Recall (from the end of chapter 11) that a wrapper class defines objects that hold a primitive value. An object of class Integer holds an int and some methods.

The output of the program is (for a computer in the US):

integer = 7,654,321 
 double = 11,000.001

Again, the locale of your computer affects the format. Also, notice that the output for the double is rounded.


QUESTION 4:

Would the following program fragment work?

Integer a = 123;
Integer b = 34;
System.out.println(  numform.format(a) + " + " + numform.format(b) + " = " + a+b );

go to previous page   go to home page   go to next page