Java-11.3 formatted output (1)

Source: Internet
Author: User

Java-11.3 formatted output (1)

In this chapter, we will discuss formatting and output. We will use two chapters to describe this topic.

In the C language system, the most used estimation is the printf function:

 

printf(%d%f,a,b)

In the preceding simple sentence, formatted output is used. % d indicates the output integer, and % f indicates the floating point number.

 

1. System. out. printf and System. out. format

Java inherits the C language system, and of course there will be functions such as printf. The following is an example:

 

package com.ray.ch11;public class Test {public static void main(String[] args) {int a = 1;String b = b;System.out.printf(%d%s, a, b);System.out.println();System.out.format(%d%s, a, b);}}

Output:

 

1b

1b

In the above Code, System. out. printf and System. out. format are equivalent.

 

2. Formatter class

 

Package com. ray. ch11; import java. util. formatter; public class Test {private Formatter formatter = new Formatter (System. out); // The output field public void print (int a, String B) {formatter. format (% d % s, a, B);} public static void main (String [] args) {int a = 1; String B = B; new Test (). print (a, B );}}

 

Output:

1b

When using the Formatter class, you must note that it must define the output. Otherwise, although the output of the string already exists in memory, there is no output. The above is output on the console, so we put System. out in it.

 

Summary: This chapter briefly describes two aspects of formatting output: one is the simplest printf function and the other is the Formmater class.

 

 


 

 

 

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.