Formatted output
In which cases the formatted output is used:
- The use of formatted output in the abnormal print to log is helpful to troubleshoot the cause of the error;
printf format
Example:
public class PrintfTest { public static void main(String[] args) { printfTest1("小明",123456); } private static void printfTest1(String name, Integer fansCount) { System.out.printf("你好,%s,粉丝%d人",name,fansCount); } }
PrintfTest1 () Description:
- %: Characters starting with% will be replaced with corresponding parameters;
- S: Represents a string;
- D: Represents a decimal integer.
Other translator descriptions:
| Conversion character |
type |
| D |
Decimal integer |
| X |
hexadecimal integer |
| O |
Eight-binary integers |
| F |
Single-precision floating-point number |
| E |
Exponential floating-point number |
| G |
Universal floating point number |
| A |
Hexadecimal floating-point number |
| S |
String |
| C |
Character |
| B |
Boolean |
| H |
Hash code |
String.Format formatting
Difference: String.Format does not print output, the other uses the same as printf.
Example:
// 作为一个变量,可以传入日志info或error中,方便排查bug String temp = String.format("%1$s ,测试, %2$s","小明","测试");
This is my first article, but also the first time to use Markdown, the composition of the article needs to be slowly pondering. has been practicing Java development for a year, due to the weak foundation, the development of the hole more and more, taking advantage of this time to re-understand the basic knowledge of Java.