Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Link
Courses Plus Student 38 PointsSystem.out.println
I know the code they gave for the "Hello World" program was as follows; import java.io.Console;
public class Introductions {
public static void main(String[] args) {
Console console = System.console();
// Welcome to the Introductions program! Your code goes below here
console.printf("Hello, my name if Craig");
} }
Why was "System.out.println" not used?
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! My best guess, is that they might have planned to do something slightly different here, but changed their minds? The "f" in printf
stands for "format". We generally use it when we want to use placeholder tokens that will later contain the string variant of the value of a variable. That wasn't actually needed in this example and he could have just as easily used print
or println
.
Here is some documentation on print/println/printf from Oracle.
Hope this helps!
Lucas Brown
2,893 PointsTo add to Jennifer's answer, printf is used over println so it wouldn't have to be changed later. In the next video, you'll continue using the same code and use format specifiers to insert a variable into a string, which wouldn't work with println. It is possible use concat in println ( System.out.prinln( firstName + " rest of string" ); ) to achieve the same goal, but I personally find printf cleaner and easier to use, and there may be other advantages that are beyond the scope of this course.
I think your real question is why we're using System.out.printf over console.printf. I did some searching and decided the difference is beyond what we'll need to know for this course - as far as we're concerned, they do essentially the same thing.