转自 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
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