是否有充分的理由使用“printf”而不是“打印”在java? [英] Is there a good reason to use "printf" instead of "print" in java?

查看:134
本文介绍了是否有充分的理由使用“printf”而不是“打印”在java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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?

推荐答案

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

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

printf <的格式/ code>使用 Formatter 类'格式化语法

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

printf 当在一行中显示多个变量时,该方法特别有用,这将是繁琐的g string concatenation:

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

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);

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

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.

这篇关于是否有充分的理由使用“printf”而不是“打印”在java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆