search
HomeJavajavaTutorialWhat are the ways to format output in Java?

    Java console output

    1. Use System.out.write method output

    You can output information to the console The write method using the output stream is just not as convenient to use as print and println. In fact, the print and println methods also encapsulate write and ultimately call write to write data to the console.
    When using write, you need to import IOException, that is, add import java.io.IOException in front of the java file; the write method may throw IOExcetion.
    I won’t introduce the write method in detail. It is better to use print or println usually.

    2 Use the System.out.println method to output

    To view it in idea, enter System.out .println() actually calls the following methods (function overloading)

    What are the ways to format output in Java?

    After calling System.out.println(), a newline will be added directly after the output. If you do not write parameters, it will only serve as a line break.

    If the parameter is char[], all elements in char[] will be output directly without any separators between the elements, as if a string is output.

    Another special parameter is Object. Object is the parent class of all classes in Java. If the parameter is an Object object, the address of the Object or null will be output directly unless the corresponding Object is overridden. I have my own toString method, so I won’t go into too much detail here.

    We can use string concatenation operations to output various formats, such as output: x=5, output the value of x

    int x=5;
    System.out.println("x="+x);

    3 Using System The .out.print method output

    is only one less method without parameters than System.out.println().

    The only difference between System.out.print and System.out.println is that this one does not add newlines.

    What are the ways to format output in Java?

    4 System.out.printf

    The bottom layer of printf is format. There is no difference in the usage of the two methods. In fact, printf just calls Just format it

    What are the ways to format output in Java?

    Detailed introduction to formatted output

    System.out.format(); System.out.printf();

    The usage of both is the same. The format is used as an example below.

    When we need to format and output some data, just using System.out.println(); is not enough. At this time, we need to use the format method

    System .out provides the formatted output method format. The first parameter of format is a template. The template has some placeholders. The following parameters are used to replace the placeholders in the template to complete the output of the entire information.

    For example:

    String name="LiLi";
    int age=8;
    char sex='男';
    float grade=52.5;
    System.out.format("姓名:%s, 年龄:%d, 性别:%c, 成绩:%.1f\n", name, age, sex, grade);

    Output: Name: LiLi, Age: 8, Gender: Male, Score: 52.5

    %c, %s, etc. are called placeholders symbols, representing characters and strings respectively, which will be replaced by the values ​​of the corresponding variables that appear later. Like %.1f, .f is added to %f to indicate outputting a 1-digit decimal.

    Commonly used placeholders:

    Placeholder Description
    %d Formatted output integer
    %f Formatted output floating point number
    %e Format floating point number in scientific notation
    %s Format output string

    当然不可能只有这么点了,下边才是大头????

    格式化整数

    既可以格式化基本数据类型,也可以格式化它们的包装类

    • %d :格式化十进制整数

    • %o :格式化八进制整数

    • %x :格式化小写十六进制整数,如:abc58

    • %X :格式化大写十六进制整数,如:ABC58

    修饰符
    • “+”:格式化正整数时强制添加上正好,如%+d可将123格式化位+123

    • “,”:格式化整数时按“千”分组,例如:%,d可将1234567890格式化位1,234,567,890

    它俩可以组合使用,如:%+,d

    数据的宽度

    %md或%-md指定格式化整数的长度最少为m

    如%6d可将123格式化为“ 123”,左边三个空格,即如果数字的长度不足6,则在格式化的数字左边添加空格使得整体长度为6(左边添加空格即右对齐

    %-6d使得结果左对齐,即在右边添加空格,如%-6d可将123格式化为“123 ”,右边三个空格

    格式化浮点数

    既可以格式化基本数据类型,也可以格式化它们的包装类

    修饰符
    • “+” :格式化正数时添加正号

    • “,”:将正数部分按千分位

    它俩可以组合使用,如:%+,f

    限制小数位数与宽度
    • “%.nf”可以限制小数的位数为n(遵循四舍五入)

    • “%mf”可以规定数据的宽度为m,长度不足补空格,默认居右(%-mf,居左)

    组合使用:

    System.out.printf("%7.3f",0.1235);

    结果:0.124 (还有俩个空格,“.”也占一个长度)

    String也可以调用format方法

    String也可以调用format方法,用法与上述完全一致,结果返回一个字符串对象

    例:

    String s=String.format("%d",12345);

    s的值为12345

    The above is the detailed content of What are the ways to format output in Java?. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

    Start Spring using IntelliJIDEAUltimate version...

    How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

    When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

    How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

    How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

    How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

    Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

    How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

    Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

    E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

    Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

    How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

    How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools