Important Java Concept

Java Printf() Method for Formatting

In this article, we will learn about the Java printf() method, and let’s also understand the printf() method by using various examples. printf() method is available in PrintStream class. This method works similarly to the printf() function in the c language.

Syntax

The syntax for printf() statement is:

System.out.printf(format, args);

There are many format characters used in the printf() method, some of them are :
%s – string
%c – char
%d – decimal integer
%f – float number
%o – octal number(base 8)
%b, %B – boolean value
%x, %X – hexadecimal number
%e, %E – exponential floating point numbers
%n – new line character

Sample Java Code

    public static void main(String[] args) {
        String s1 = "Hello";
        int n = 65;
        float f = 15.1234f;
        System.out.printf("string = %s%nnum = %d%nhexa decimal = %x%nfloat = %f",
                s1, n, n, f);
    }

Output

   string = Hello
   num = 65
   hexa decimal = 41
   float = 15.123400

Explanation

In the above java program, we have declared a few variables s1 as a string, n is declared as an integer, and f as a floating-point number.

System.out.printf("string = %s%nnum = %d%nhexa decimal = %x%nfloat =   %f", s1 , n,n,f);

Here, the string ‘string = ’ will be displayed as it is in the output. After this, we have %s, which represents the format characters to display a string, i.e., s1 value. As s1 was given Hello as input, in the output statement ‘Hello’ will be printed.
After string = instead of declaring %s if we declare %d then this will be a wrong format and an error message is displayed as an Invalid format.

Formatting a Boolean value

Let us understand formatting a boolean value using a sample code.

Example 2

    public static void main(String[] args) {
        boolean ex1 = true;
        boolean ex2 = false;
        System.out.printf("ex1 = %b%nex2 = %b%n %b", ex1, ex2, null);
    }

Output

  ex1 = true
  ex2 = false
  false

Explanation

The value of ex1 is set to true and the value of ex2 is set to false. In the printf() method %b is used because we want the output of type boolean and if we want to print boolean value for null it will be false by default only.

Formatting Character, Exponential Values

Let us understand the concept of formatting a character and exponential values using the below java code.

Displaying Formatted Output using String.format()

If we want only a string that consists of formatted output, then we can take the help of the format() method for the String class. The format characters supported by System.out.printf() are also usable in the format() method. format() method is a static method, we can call it a String.format(). Let us understand this topic by using a simple java code

    public static void main(String[] args) {
        int i = 55;
        String s = "hello";
        char ch = 'A';
        String str = String.format("i=%d%ns=%s%nch=%c",i, s, ch);
        System.out.println(str);
    }

Output

i=55
s=hello
ch=A

Conclusion

From the above article, we have learned about what is printf() method, the syntax to write a printf() method, the various format characters used by the printf() method, how to use format characters in java code, and how to display formatted output using String.format() method.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?