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 trial

Java Java Basics Getting Started with Java Strings, Variables, and Formatting

I prefers System.out.println(); to print the text but iam getting confused of console.printf(); How can i get it??

I learned c languge before 2 months ago and after that i started to learn java.I sawed videos of java on youtube Where i learned oops concept. They use System.out.println(); to print the text but after coming here iam getting confused whether to use This or that. if iam using that iam getting error Why????

Name.java
// I have setup a java.io.Console object for you named console


  public class Name{

   public static void main(String[]args){

   String firstName="Syed khaleel";

   System.out.println(firstName);

   }  





  }

1 Answer

Hello

the f in printf is "Formatted"

print f allows you to format your print statement. LEt take a look at this:

String firstName = "Mark"; String food = "Pizza"; int times = 5;

System.out.println("%s loves to eat %s%n%d times a day", firstName , food, times);

or you could do

System.out.println(firstName + " loves to eat " + food + "\n" + times + " times a day" );

Both will do the same thing ... but the first one is more elegant. I can look at it and tell the types of the arguments (String, and int) without having to scroll up the code to find the type.

it a matter of taste.

I typed this mind to screen ... there maybe some typos ... but I think you got the idea.

If this answers your question, please mark as answered.