package com.tij;
//格式化输出 TIJ P289
public class SimpleFormat {
public static void main(String[] args) {
int x=5;
double y=4.233423;
//old way
System.out.println("Row 1: ["+x+" "+y+ "]");
//new way
System.out.format("Row 1: [%d %f]\n",x,y );
//or
System.out.printf("Row 1: [%d %f]\n",x,y );
}
}
/*
output:
Row 1: [5 4.233423]
Row 1: [5 4.233423]
Row 1: [5 4.233423]
*/
可以看出format()与printf()是等价的,它们只需要一个简单的格式化字符串加上一串参数即可,每个参数对应一个格式修饰符。