Printf Java : Printing Formatted Messages on Screen

java iteratorJava is one of the world’s most popular programming languages.  It was first developed by James Gosling of Sun Microsystems. This exciting technology was released in 1995 and since then its use has grown by leaps and bounds. Java is ideal for the distributed environments particularly the Internet. Programmers use it to develop web based applications as well as standalone applications.  Java is an object-oriented and high level programming language. C++ developers will find it relatively easy to migrate to this platform. Java’s mission for developers is “Write Once, Run Anywhere”.   This platform is known to be fast, secure as well as reliable. Java is easy to learn and has a syntax somewhat similar to C++. Programs written in Java are compiled to machine independent byte code. Then the latter is interpreted by Java Virtual Machines(JVM) to the byte code of the platform it is using.  Important features of this language are  support for multi-threading and automatic memory management.

In this beginner’s level tutorial we walk you through the Java printf() function. We assume that you are familiar with the basics of programming. If not you may want to take this introductory course to Java programming.

The printf function is common across most programming languages, and is used to display any statement on the screen. It has multiple formatting options that can be used to format the variable or string you want to print, in various ways.

Formatting using the System.out.printf() Function

In Java the syntax of this method is as follows:

System.out.printf( “format string” [,arg1, arg2, ... ] );

The first argument is either a literal or a format specifier. Only if format specifiers are present are arguments used. Flags, precision, width and conversion characters are the format specifiers. Given below is the sequence of format specifiers.

% [flags][width][.precision]conversion-character

  • Flags- The default behavior is right justification of the output. Use ‘-‘ character for left justification. In case, numerical values are not long enough to meet the field length, by default blank padding is done. Use 0 character to ensure 0 padding.
  • Width- It specifies the field width for corresponding argument. You can also say, it defines the minimum number of characters of the output.
  • Precision- This value is preceded by the period operator. In case of floating point values, the precision value determines how many decimal characters are printed.
  •  Conversion Characters are listed below. Take a look at each one of them.
  1. ‘d’ is used for the different type of integers such as byte, int, short, long.
  2. ‘f’ is used for different of floating point numbers such as float and double.
  3. ‘c’ is used for character values
  4. ‘s’ is used for strings.
  5. ‘n’ is used for newline.

You can learn more about printf formatting in Java with this course.

Exampl 1: A Simple Java Program using System.out.printf()

public class sample{

public static void main(String args[]) {

System.out.printf("Welcome to this Java programming tutorial");



The keyword class defines sample to be a new type of class. The public keyword used means that the class can be accessed by all. The main() function starts the execution of the Java program. It does not return any value and is class specific. This means there can be only one instance of main.  The System.out.printf() function will print the string passed as the parameter to it. Every statement in a Java program is terminated by a semi-colon. Take this course to learn how to write your own Java programs.

Integer formatting

Suppose we declare an integer variable and initialize it to the value 1234.

  • Int num =1234;
  • The output will be the integer as it is i.e the value 1234.

This specifies the field width as 6 characters. As the integer 1234 occupies only 4 spaces, there will be 2 leading blank spaces in the output.

  • System.out.printf(“%-6d”,num);

This is similar to the previous example. The difference is here 1234 will be output first followed by 2 trailing blank spaces.

  • System.out.printf(“%06d”,num);

Here the field width is 6 characters. Here instead of leading spaces there will be 2 leading zeroes.

  • System.out.printf(“%6.2d”,num);

Here the precision is 2 characters while the field width is 6 characters. Only the first 2 digits of the integer will be printed.

Floating Point Formatting

Let’s declare a floating point variable and initialize it to a floating point value in Java. Then we’ll see how we can print it out using different formatting options.

Float float1;
Float1 = 12.3456;
  • System.out.printf(“%f”,float1);

This will output the floating point value as it is.

  • System.out.printf(“%8f”,float1);

This specifies the field width as 8 characters. However the number has only 6 charcters. Hence the output will be padded on the left by 2 blank spaces.

  • System.out.printf(“%.6f”,float1);

Here the precision is 6 characters. Hence maximum 6 decimal digits of the floating point number will be printed.

  • System.out.printf(“%8.4f”,float1);

Here the field width is 8 characters. Precision is 4 characters. Hence maximum 4 decimal digits of the number will be printed. The output will occupy minimum 8 characters.  Since the float1’s value has only 6 characters there will be 2 leading space characters in the output.

String Formatting

Here we declare a String object and initialize it.

String Str1;

Str1 = “ HelloWorld”.

  • System.out.printf(“%s”,Str1);

This will print the string as it is.

  • System.out.printf(“%12s”,Str1);

Here the field width is 12 characters. As Str1 has only 10 characters it will have 2 leading spaces.

  • System.out.printf(“%-12sf”,Str1);

This is similar to the previous example. The difference is it will have 2 trailing spaces.

  • System.out.printf(“%.8s”,float1);

Here the precision is 8 characters.  Hence maximum 8 characters of the string will be printed. As Str1’s value has 10 characters the last 2 characters will be omitted from the output.

There is another way to use printf in the java programming language. This is the method. Its parameters are identical to that of System.out.printf(). However, it returns a PrintStream object which is an output stream. If the format parameter is null then, this method throws a NullPointerException. For other errors, in the parameters the IllegalFormatException is thrown.

The third way provided by Java to use printf() method is the method. Its parameters and functioning are identical to that of System.out.printf()method. However, it returns a Console object. In the event of any errors in its parameters, this particular method throws IllegalFormatException.

Hope this tutorial was interesting and useful. Do study the given code and experiment with it. To master any programming concept, you should write your own programs and play around with them.Once you’re ready to move on to the next level, you can take this advanced Java course to learn more in depth techniques.