【翻译】【技术】Java入门:print(), println()和printf()的区别(翻译自treehouse问答)
2017-02-23 10:56
513 查看
转自 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
MOD
Allan Clark
almost 2 years ago
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
相关文章推荐
- Java中print、printf、println的区别 详解
- Java中print、printf、println的区别 详解
- Java:print、printf、println的区别
- Java中print、printf、println的区别
- Java中print、printf、println的区别
- Java中print、printf、println的区别
- Java学习-System.out.println,system.out.print,system.out.printf的区别
- 【Java学习笔记 】print、printf、println的区别
- Java中print、printf、println的区别详解
- Java中print、printf、println的区别(转)
- java 中print 与printf与println的大致区别
- java中printf,print,println,区别及用法详解
- Java中print、printf、println的区别 (
- 浅析Java中print、printf、println的区别
- Java中print、printf、println的区别详解
- java中print\println\printf的区别
- Java中print、printf、println的区别
- 浅析Java中print、printf、println的区别
- Java学习笔记1:Java中有关print、println、printf的用法和区别
- java中print,printf,println区别