Home » Java Tutorial » Printf Java Using Format Specifier

Printf Java Using Format Specifier

The printf function in Java is the function of the Java printstream class. We are familiar with printf function used in C programming. Java also provides printf function for printing formatted strings. To print a string, java provides print() and println(). But both the function take only one argument that is a string to print it on the console. On the other hand, printf takes multiple arguments to print strings in a specific format. In some cases, it is essential to print formatted strings. For that matter Java printf function is useful.

For printing formatted string, Java provides one more function, namely string.format(). But this function returns the formatted string. The printf function uses a class of format function, i.e., java.util.Formatter to parse the string and generate output.

How to use printf in Java?

There are three ways to use the printf

  1. printf(string)
  2. printf(string format, object)
  3. printf(locale, string format, object)

Parameters

Locale – The locale to apply during formatting. If it is null, then no localized is applied.

Format – Format specifier for the string.

Object – The value to be print at the place of the format specifier.

Format specifiers

Below are the format specifiers for printf function.

  1. %c for a character.
  2. %d or %i for integer.
  3. %e for exponential floating-point number.
  4. %f for a float number.
  5. %o for octal numbers.
  6. %s for a string.
  7. %u for unsigned decimal (integer) number.
  8. %x for hexadecimal numbers.
  9. %t to formats date/time.
  10. %n to print on a new line

Escape sequence

  1. \b –  for backspace.
  2. \f – The next line will start from the last character of the current line.
  3. \n – For newline.
  4. \r – For carriage return.
  5. \t – For tab.

String formatting using printf in Java

Example

public class printf {

    public static void main(String[] args) {
        String str = "Prad tutorials";
        System.out.printf(str+"\n"); //prints string
        
        System.out.printf("hello %s\n",str); // prints string after hello
        
        System.out.printf("'%20s'\n",str); //adds white spaces before string. Length of Prad tutorials is 14. so this will add 6 white spaces
        
        System.out.printf("'%-20s'\n",str); // Same as above only minus sign will adds white spaces after string.
        
        System.out.printf("%S %s\n","hello", str); // %S will print hello in uppercase letters.
        
    }
    
}

Output

Prad tutorials
hello Prad tutorials
'      Prad tutorials'
'Prad tutorials      '
HELLO Prad tutorials

Formatting char using printf in Java.

Example

public class printf {

    public static void main(String[] args) {
        
        char c = 'p';
        System.out.printf("%c%n",c); // %c will print character in lower case format
        
        System.out.printf("%C%n",c); // %c will print character in upper case format
        
    }
    
}

Output

P
P

Formatting integer using printf in Java.

Example

import Java.util.Locale;

public class printf {

    public static void main(String[] args) {
        
        System.out.printf("%d\n",1024); // prints number on console

        System.out.printf(Locale.US,"%,d\n",500000); //prints number in US locale
        
        System.out.printf(Locale.ITALY,"%,d\n", 100000); //prints number in italy locale   
    }
}

Output

1024
500,000
100.000

Formatting floating-point number using Java printf function.

Example

public class printf {

    public static void main(String[] args) {
        
        System.out.printf("%f\n",4.293471); //prints floating point number
        
        System.out.printf("%.2f\n",9.257982); //prints 2 digits after floating point
        
        System.out.printf("'%6.3f'\n",8.24343); // adds white space same as string
        
        System.out.printf("%.2e\n",6.29487); //prints output in scientific notation
        
        System.out.printf("'%-10.2e'\n",3.25893); // adds white spaces after number
    }
    
}

Output

4.293471
9.26
' 8.243'
6.29e+00
'3.26e+00  '

Formatting date using Java printf function.

For printing date and time, we use some characters to extract particular data from the object of the date class.

  1. H, M, and S these characters are used to extract hours, minutes, and second, respectively.
  2. L and N are used to extract milliseconds and nanoseconds.
  3. T for printing time in (hours:minutes:seconds) format.
  4. p is used to extract a.m. and p.m.
  5. z is used to extract the time zone.

To avoid a passing number of parameters to the function, we can also use $1 in the format specifiers. An example is given below.

Example

import Java.util.Date;

public class printf {

    public static void main(String[] args) {
        
        Date ob = new Date();
        
        System.out.printf("%tT\n",ob);
        
        System.out.printf("Hours:%tH Minutes:%tM Seconds:%tS \n",ob,ob,ob);
        
        System.out.printf("Hours:%tH Minutes:%tM Seconds:%tS Milliseconds:%tL Nanoseconds:%tN \n",ob,ob,ob,ob,ob);
    
        System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", ob);
    }
    
}

Output

19:07:01
Hours:19 Minutes:07 Seconds:01 
Hours:19 Minutes:07 Seconds:01 Milliseconds:175 Nanoseconds:175000000 
19:07:01 pm 175 175000000 +0530
What does printf do in Java?

The printf function takes arguments and prints the string on the console in a specified format. To specify a particular format, Java provides many format specifiers to help in printing given strings.

Do you know?
1. Wrapper class in Java
2. This Keyword in Java
3. Bubble Sort in Java
4. Armstrong Number in Java
5. Method overloading in java
6. Prime Number Program in Java
7. Type Casting In Java
8. Ternary Operator in Java
9. Immutable Class in Java
10. Printf Java

Pin It on Pinterest