【翻译】【技术】Java入门:print(), println()和printf()的区别(翻译自treehouse问答)

转自  https://teamtreehouse.com/community/what-is-the-difference-between-printf-and-println-and-what-does-the-term-console-actually-mean

稍后有空翻译。。。will come soon!

-------------------------附原文:

What is the difference between printf and println? And what does the term "console" actually mean?

Hi treehousers,

I have been learning java with treehouse for the past few days and I'm confused about the difference between "printf" and "println". Furthermore, I also don't know what does the term "console" mean and what it is for? sometimes people use "console.printf" and "System.out.printf". What are they?

Thanks before!

3 Answers

Allan Clark
MOD

First for the difference in println and printf. The names are short versions of their function. println is short for "print line", meaning after the argument is printed then goes to the next line. printf is short for print formatter, it gives you the ability to mark where in the String variables will go and pass in those variables with it. This saves from having to do a long String concatenation. Here are a few examples, I will be putting '_' where the computer is left and where it would put stuff if sent another print statement.

Base print statement:

System.out.print("What do you mean Darth Vader's my father?!?");

Result printed to the User in the console:

What do you mean Darth Vader's my father?!?_

println statement:

System.out.println("What do you mean Darth Vader's my father?!?");

Result printed to the User in the console:

What do you mean Darth Vader's my father?!?
_

printf statment:

good resource: http://alvinalexander.com/programming/printf-format-cheat-sheet

System.out.printf("What do you mean %s is my father?!?", dad);

What do you mean Darth Vader's my father?!?_

*side note: this assumes there is a String variable names dad = Darth Vader's

As for console vs System.out: In general the console is like (and can be) the command prompt in Windows very basic text input/output.

System.out is telling Java that you want to use the output steam to the console.

The word console is used in code basically as a wrapper for System.out. Some TreeHouse challenges have code that is abstracted away from you. This includes the declaration of a console variable. IIRC the declaration will have System.out in it.

Hope this helps, let me know if I can clear anything up.

May the Java force be with you


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值