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

danny engels
danny engels
366 Points

call the printf method on the console

i m not so clear what the mean whit printf methode

Name.java
// I have setup a java.io.Console object for you named console
  String firstName = "danny";
  console.printf("my name is danny\n",firstName);

1 Answer

The printf method on the console object will print a formatted string to the console. You pass it a string which can contain placeholders that will be replaced by any values or variables that are passed in as arguments to the method.

For example:

// I have setup a java.io.Console object for you named console
String firstName = "Danny";
console.printf("%s can code in Java!", firstName);

Will print Danny can code in Java! to the console. The %s is a placeholder that will stand in for a string. Since it is the first (and in this case only) placeholder in the string, the first variable (which is firstName) will get placed into its spot.

We would use %s in this example because the variable we will be putting in its spot is a string. If it were an int data type we could use %d for 'decimal', or if it was a float data type we could use %f.