Tame Tiger: Formatted output

Source: Internet
Author: User

The original plan for J2SE version 1.4 is to include support for formatted output. These features are not included in the published release because of the time limit and the ability to make the release of the new version impossible. Now with Tiger, there is an intrinsic support for printing formatted strings.

For those who have never contacted C from the start with Java programming, or, for those who do not have enough knowledge of C, the format string is odd text strings that specify the output characteristics of a set of variables. Instead of connecting strings together with a plus sign (such as FirstName + "+ lastName"), instead of providing a string description output and supplying arguments to the placeholder in the replacement string at the end of the method call: string s = String.Format ("%1 $ s)%2$s ", FirstName, LastName).

Formatter class

First, let's analyze the new Java.util.Formatter class. You may not use this class directly, but it provides an internal mechanism for formatting. In the Javadoc of this class, you see a table that describes the supported formatting options. The range of options ranges from specifying the precision and digits of floating-point numbers in a format such as%7.4f, to the%tt of the formatted time, to the format of the third parameter%3$s.

Formatted output with Formatter is a two-step process: Create a Appendable object to store the output, and use the format () method to place the formatted content in the object. The following is a list of the implementations of the Appendable interface:

BufferedWriter

Chararraywriter

Charbuffer

FileWriter

Filterwriter

LogStream

OutputStreamWriter

PipedWriter

PrintStream

PrintWriter

StringBuffer

StringBuilder

StringWriter

Writer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.