Java printf Example

In this article, we will demonstrate the use of printf() method in Java.

Java printf() method overview

Well, the printf() method is not only there in C, but also in Java. This method belongs to the PrintStream class. It’s used to print formatted strings using various format specifiers.

Syntax

Following are the syntaxes available for the printf() method:

System.out.printf(string);
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);

Format Specifiers

Let’s look at the available format specifiers available for printf() method:

%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s String
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%t formats date/time
%% print a percent sign
\% print a percent sign

Note: %n or \n are used as line separators in printf().

Escape Characters

Following are the escape characters available in printf():

\b backspace
\f next line first character starts to the right of current line last character
\n newline
\r carriage return
\t tab
\\ backslash

Number Formatting Example

public class Main{

     public static void main(String []args){
        int x = 20;
        System.out.printf("Formatted output is: %d %d%n", x, -x);
        
        float y = 2.28f;
        System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y);
        float z = 3.147293165f;
        System.out.printf("Precision formatting upto 2 decimal places %.2f\n",z);
     }
}

Output:

Formatted output is: 20 -20
Precision formatting upto 4 decimal places 2.2800
Precision formatting upto 2 decimal places 3.15

String Formatting Example

public class Main{

     public static void main(String []args){
       System.out.printf("%s %s!%n","Hello","World");
       System.out.printf("%s\f%s!%n","Hello","World!");
       System.out.printf("%s\\%s!%n","Hello","World!");
       System.out.printf("%s %S!%n","Hello","World");
     }
}

Output:

Hello World!
HelloWorld!!
Hello\World!!
Hello WORLD!

Boolean Formatting Example

public class Main{

     public static void main(String []args){
        System.out.printf("%b%n", false);
        System.out.printf("%b%n", 0.5);
        System.out.printf("%b%n", "false");
     }
}

Output:

false
true
true

Time Formatting Example

import java.util.Date;

public class Main{

     public static void main(String []args){
        Date date = new Date();
        System.out.printf("%tT%n", date);
        System.out.printf("H : %tH, M: %tM, S: %tS%n",date,date,date);
     }
}

Output:

13:48:39
H : 13, M: 48, S: 39

Date Formatting Example

import java.util.Date;

public class Main{

     public static void main(String []args){
        Date date = new Date();
       System.out.printf("%s %tB %<te, %<tY", "Current date: ", date);
       System.out.printf("%1$td.%1$tm.%1$ty %n", date);
       System.out.printf("%s %tb %<te, %<ty", "Current date: ", date);
     }
}

Output:

Current date:  April 24, 202224.04.22 
Current date:  Apr 24, 22

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments