Output Formatting in Java using printf()

In this tutorial, we will learn how to formatting and displaying output in Java using printf() and format() methods.

Java PrintStream class provides a method named printf() that works similar to printf() function in C.

The printf() method is a convenient method to write formatted text (or output) to console.

It writes a formatted string to the underlying output stream using the specified format string and arguments. It returns the output stream. From Java 5 onwards, it is available.

The general syntax to use printf() method is as:

public PrintStream printf(String format, Object... args)

This method contains two parameters:

  • format – a format string that contains at least one format specifier for each of the argument. The format specifier just defines how data is to be presented on the console.
  • args – Argument values referenced by the format specifier in the format string. There may be many argument values following the first string argument, which can be used in method invocation.

The printf() method can throw:

  • IllegarArgumentException – If a format string contains an illegal format specifier that is incompatible with the specified arguments, can generate IllegalArgumentException.
  • NullPointerException – If the format is null.

How to call printf() Method in Java?

We know that System.out returns PrintStream class object. So, to call the printf() method, we can use System.out.printf() to create a formatted content.

We can use the following format specifiers in printf() method. They are as:

  • %s – string
  • %c – char
  • %d – decimal integer
  • %f – float number
  • %o – octal number
  • %b, %B – boolean value
  • %x, %X – hexadecimal number
  • %e, %E – number in scientific notation
  • %n – new line character

As you can see, a format specifier begins with ‘%’ and ends with a conversion-type character (e.g. “%d” for decimal integer, “%f” for float and double numbers).

Formatting and Displaying Output with System.out.printf()

Consider a simple example for using printf() method below.

System.out.printf("Salary = %f", sal );

In the above statement, the string Salary = will display on the console as it is in the output. After it, we have used %f that represents a format specifier to display a float value, i.e. sal value.

Therefore, if the variable sal has 9975.89, then the output will display by the above statement like this:

Salary = 9975.89

Let us write a Java program to create a formatted output using printf() method of PrintStream class and display it on the console.

Program code 1:

package javaProgram;
public class FormattedOutput {
public static void main(String[] args)
 String str = "Hello Java";
 int num = 99;
 float f = 20.25555f;
 System.out.printf("String = %s %nnum = %d %nfloat = %f ", str, num, f);
      String = Hello Java 
      num = 99 
      float = 20.255550 

Program code 2:

package javaProgram;
public class FormattedOutput {
public static void main(String[] args)
 String name = "John";
 int age = 20;
 System.out.printf("My name is %s and my age is %d", name, age);
      My name is John and my age is 20

Displaying Formatted Output with String.format()

If you want only a string that comprise formatted output, you can take the help of format() method of String class.

The format specifiers supported by System.out.printf() are also usable with format() method. Since format() method of String class is a static method, you can call it as:


The above statement returns a string that contains a formatted output.

Let us write a Java program for getting the formatted output in the string form.

Program code 3:

package javaProgram;
public class FormattedOutput {
public static void main(String[] args)
  String text = "Hello Java";
  int num = 699;
  char ch = 'B';

// Formatting output and getting in the string form. 
   String s = String.format("Text = %s %nnum = %d %ncharacter = %c", text, num, ch);
      Text = Hello Java 
      num = 699 
      character = B

In this tutorial, you learned how to formatting output in Java using printf() and format() methods of PrintStream and String classes, respectively. Hope that you will have understood the basic concept of formatting output in Java.
Thanks for reading!!!
Next ⇒ ObjectInputStream in Java⇐ Prev Next ⇒

Please share your love