The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: I/O

Formatting

Stream objects that implement formatting are instances of either PrintWriter (in the API reference documentation), a character stream class, and PrintStream (in the API reference documentation), a byte stream class.

Note:  For historical reasons, System.out (in the API reference documentation) and System.err (in the API reference documentation) are PrintStream (byte stream) objects instead of PrintWriter (character stream) objects. System.out and System.err should be the only PrintStream objects you ever need to use. When you need to create a formatted output stream, instantiate PrintWriter, not PrintStream.

Like all byte and character streams objects, instances of PrintStream and PrintWriter implement a standard set of write methods for simple byte and character output. In addition, both PrintStream and PrintWriter implement the same set of methods for converting internal data into formatted output. Two levels of formatting are provided:

The print and println Methods

A call to print or println outputs a single value, after converting the value using the appropriate toString method. We can see this in the Root (in a .java source file) example:
public class Root {
    public static void main(String[] args) {
        int i = 2;
        double r = Math.sqrt(i);
        
        System.out.print("The square root of ");
        System.out.print(i);
        System.out.print(" is ");
        System.out.print(r);
        System.out.println(".");

        i = 5;
        r = Math.sqrt(i);
        System.out.println("The square root of " + i + " is " + r + ".");
    }
}
Here is the output of Root:
The square root of 2 is 1.4142135623730951.
The square root of 5 is 2.23606797749979.
The i and r variables are formatted twice: the first time using code in an overload of print; the second time by conversion code automatically generated by the Java compiler, which also utilizes toString. can format any value this way — but you don't have much control over the results.

The format Method

The format method formats multiple arguments based on a format string. The format string consists of static text embedded with format specifiers; except for the format specifiers, the format string is output unchanged.

Format strings support many features. In this tutorial, we'll just cover some basics. For a complete description, see format string syntax (in the API reference documentation) in the API specification.

The Root2 (in a .java source file)example formats two values with a single format call:

public class Root2 {
    public static void main(String[] args) {
        int i = 2;
        double r = Math.sqrt(i);
        
        System.out.format("The square root of %d is %f.%n", i, r);
    }
}
Here is the output:
The square root of 2 is 1.414214.
Like the three used in this example, all format specifiers begin with a % and end with a 1- or 2-character conversion that specifies the kind of formatted output being generated. The three conversions used here are: Here are some other conversions: There are many other conversions.

Note: Except for %% and %n, all format specifiers must match an argument. If they don't, an exception is thrown.

In the Java programming language, the \n escape always generates the linefeed character (\u000A). Don't use \n unless you specifically want a linefeed character. To get the correct line separator for the local platform, use %n.


In addition to the conversion, a format specifier can contain several additional elements that further customize the formatted output. Here's an example, Format (in a .java source file), that uses every possible kind of element.
public class Format {
    public static void main(String[] args) {
        System.out.format("%f, %1$+020.10f %n", Math.PI);
    }
}
Here's the output:
3.141593, +00000003.1415926536
The additional elements are all optional. The following figure shows how the longer specifier breaks down into elements.

Elements of a format specifier

Elements of a Format Specifier.

The elements must appear in the order shown. Working from the right, the optional elements are:

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.