是否有充分的理由在java中使用“printf”而不是“print”?

时间:2021-03-10 11:49:50

I haven't had the chance to take any serious low-level programming courses in school. (I know I really should get going on learning the "behind-the-scenes" to be a better programmer.) I appreciate the conveniences of Java, including the ability to stick anything into a System.out.print statement. However, is there any reason why you would want to use System.out.printf instead?

我没有机会在学校学习任何严肃的低级程序设计课程。 (我知道我应该继续学习“幕后花絮”才能成为更好的程序员。)我很欣赏Java的便利性,包括将任何内容粘贴到System.out.print语句中的能力。但是,您是否有任何理由想要使用System.out.printf?

Also, should I avoid print calls like this in "real applications"? It's probably better to to print messages to the client's display using some kind of UI function, right?

另外,我应该在“真实应用程序”中避免这样的打印调用吗?使用某种UI功能将消息打印到客户端的显示器可能更好,对吧?

4 个解决方案

#1


The printf method of the PrintStream class provides string formatting similar to the printf function in C.

PrintStream类的printf方法提供类似于C中的printf函数的字符串格式。

The formatting for printf uses the Formatter class' formatting syntax.

printf的格式使用Formatter类的格式化语法。

The printf method can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:

当在一行中显示多个变量时,printf方法特别有用,使用字符串连接会很繁琐:

int a = 10;
int b = 20;

// Tedious string concatenation.
System.out.println("a: " + a + " b: " + b);

// Output using string formatting.
System.out.printf("a: %d b: %d\n", a, b);

Also, writting Java applications doesn't necessarily mean writing GUI applications, so when writing console applications, one would use print, println, printf and other functions that will output to System.out.

此外,编写Java应用程序并不一定意味着编写GUI应用程序,因此在编写控制台应用程序时,可以使用print,println,printf和其他将输出到System.out的函数。

#2


Typically in a "real" application you would write to a logging system such as java.util.Logging or commons logging, which have the ability to log various levels of verbosity (i.e. "debug" mode, which would be disabled in a production environment). However for quick and dirty temporary debugging sometimes System.out.println() is the easiest way to do things (assuming your runtime environment dumps standard output somewhere where you can read it).

通常在“真实”应用程序中,您将写入日志记录系统,例如java.util.Logging或commons logging,它们能够记录各种级别的详细程度(即“调试”模式,这将在生产环境中禁用) )。但是,对于快速和脏的临时调试,有时System.out.println()是最简单的方法(假设您的运行时环境将标准输出转储到可以读取它的地方)。

I should also mention that you can always use "String.format()" to create a string using a format and variable argument list (then pass this string into a logging method or System.out.println). This is often nicer than concatenating a bunch of variables together into a string.

我还应该提到,您始终可以使用“String.format()”来使用格式和变量参数列表创建字符串(然后将此字符串传递给日志记录方法或System.out.println)。这通常比将一堆变量连接成一个字符串更好。

#3


It is better if you need to control the precision of your floating-point numbers, do padding etc. System.out.print can print out all kinds of stuff, but you can't do finegrained control of precision and padding with it.

最好是你需要控制浮点数的精度,做填充等.System.out.print可以打印出各种东西,但你不能用它来精确控制精度和填充。

#4


If most real programs, logging is used. Debug messages can be turned on/off via logging configuration. See log4j as an example.

如果使用最实际的程序,则使用日志记录可以通过日志记录配置打开/关闭调试消息。以log4j为例。

#1


The printf method of the PrintStream class provides string formatting similar to the printf function in C.

PrintStream类的printf方法提供类似于C中的printf函数的字符串格式。

The formatting for printf uses the Formatter class' formatting syntax.

printf的格式使用Formatter类的格式化语法。

The printf method can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:

当在一行中显示多个变量时,printf方法特别有用,使用字符串连接会很繁琐:

int a = 10;
int b = 20;

// Tedious string concatenation.
System.out.println("a: " + a + " b: " + b);

// Output using string formatting.
System.out.printf("a: %d b: %d\n", a, b);

Also, writting Java applications doesn't necessarily mean writing GUI applications, so when writing console applications, one would use print, println, printf and other functions that will output to System.out.

此外,编写Java应用程序并不一定意味着编写GUI应用程序,因此在编写控制台应用程序时,可以使用print,println,printf和其他将输出到System.out的函数。

#2


Typically in a "real" application you would write to a logging system such as java.util.Logging or commons logging, which have the ability to log various levels of verbosity (i.e. "debug" mode, which would be disabled in a production environment). However for quick and dirty temporary debugging sometimes System.out.println() is the easiest way to do things (assuming your runtime environment dumps standard output somewhere where you can read it).

通常在“真实”应用程序中,您将写入日志记录系统,例如java.util.Logging或commons logging,它们能够记录各种级别的详细程度(即“调试”模式,这将在生产环境中禁用) )。但是,对于快速和脏的临时调试,有时System.out.println()是最简单的方法(假设您的运行时环境将标准输出转储到可以读取它的地方)。

I should also mention that you can always use "String.format()" to create a string using a format and variable argument list (then pass this string into a logging method or System.out.println). This is often nicer than concatenating a bunch of variables together into a string.

我还应该提到,您始终可以使用“String.format()”来使用格式和变量参数列表创建字符串(然后将此字符串传递给日志记录方法或System.out.println)。这通常比将一堆变量连接成一个字符串更好。

#3


It is better if you need to control the precision of your floating-point numbers, do padding etc. System.out.print can print out all kinds of stuff, but you can't do finegrained control of precision and padding with it.

最好是你需要控制浮点数的精度,做填充等.System.out.print可以打印出各种东西,但你不能用它来精确控制精度和填充。

#4


If most real programs, logging is used. Debug messages can be turned on/off via logging configuration. See log4j as an example.

如果使用最实际的程序,则使用日志记录可以通过日志记录配置打开/关闭调试消息。以log4j为例。