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.