在java中使用“printf”而不是“print”有充分的理由吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/548249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a good reason to use "printf" instead of "print" in java?
提问by tomato
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 功能将消息打印到客户端的显示器可能更好,对吗?
采纳答案by coobird
The printf
method of the PrintStreamclass provides string formattingsimilar 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
。
回答by Antti Huima
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 可以打印出各种东西,但你不能对精度和填充进行细粒度控制。
回答by Marc Novakowski
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)。这通常比将一堆变量连接成一个字符串更好。
回答by Peter Lawrey
If most real programs, logging is used. Debug messages can be turned on/off via logging configuration. See log4j as an example.
如果是大多数真实程序,则使用日志记录。可以通过日志配置打开/关闭调试消息。以 log4j 为例。