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 trialChaz Hall
1,970 Points\n and prinIn() Question
As I review everything I learned so far I noticed that \n starts a new line from a String and prinIn() states it gives you another line as well. I'm unsure why you would use one over the other. I'd appreciate any help!
1 Answer
dman10345
7,109 PointsThere's no true benefit of one over the other specifically. However \n can be a little more flexible and universal where as println is kind of concrete. Where as println offers only a new line, that is it. What about if you wanted to use printf but still needed a new line?
String name = "Dylan";
System.out.printf("Hello, my name is %s. Nice to meet you!\n", name);
This will allow you to start a new line incase you have another print statement, so it does not appear on the same line.
Another thought is what if you needed to start a new line for a bunch of statements. Well you could use multiple println statements or you could use one and separate them with the \n characters.
String name = "Dylan";
String hobby = "Programming";
String country = "USA";
System.out.printf("Hello, my name is %s, and I enjoy %s.\nI was also born and raised in the %s", name, hobby, country);
This would output the following
Hello, my name is Dylan, and I enjoy Programming.
I was also born and raised in the USA
There is a time and place for each one but as stated above \n is far more flexible and universal.
Hope this helps. Happy coding!
Chaz Hall
1,970 PointsChaz Hall
1,970 PointsSo in most cases, at least at this point in my programming career, \n has much more value and flexibility whereas printIn is limited to the functions of printf, except that it just adds a new line automatically? How would the above code look if you used a printIn statement? (I hope my terms are correct)?
dman10345
7,109 Pointsdman10345
7,109 PointsKind of, println and printf are two separate functions. Standing for print line and print format, printf allows your to use the %s characters to pass variables in as arguments.
Println simply prints a string with a new line, you can concatenate variables into it however if you'd like using the + operator.
If you were to do it in a println statement it would look like:
The above will provide the same results as the printf statement above and I'm not 100% sure but I think using two println statements would create more overheard in your program then a single printf statement, but I could be wrong. On a simplistic note, to my the println statements just look messier and less organized whereas the printf is straight forward and mapped out nicely.