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

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

问题描述

我在学校没有机会参加任何严肃的低级编程课程.(我知道我真的应该继续学习幕后"以成为一名更好的程序员.)我欣赏 Java 的便利,包括将任何东西粘贴到 System.out.print 语句.但是,您是否有任何理由想要使用 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 类提供 字符串格式化 类似于 C 中的 printf 函数.

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

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

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

printf 方法在一行显示多个变量时特别有用,而使用字符串连接会很乏味:

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
", a, b);

另外,编写Java应用程序并不一定意味着编写GUI应用程序,所以在编写控制台应用程序时,会使用printprintlnprintf 和其他将输出到 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"?而不是“打印"在爪哇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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