The format() method in Java returns the formatted string by given format, arguments, and locale. Here we have shared a few examples that explain the usage format() method.

Method signature:

The signature for a format() method is as shown below.

public static String format(String format, Object obj)
public static String format(Locale locale, String format, Object obj)

Method parameters and return type:

Return type: It returns a formatted string.

Parameters: It takes a locale as a parameter, a string, and an argument for a format string.

Throws: It throws NullPointerException and IllegalFormatException.

Format Specifier Data Type Output
%a floating-point Returns Hexadecimal output of floating-point number.
%b Any type “true” if it is not null and “false” if it is null
%c character Unicode character
%d integer (byte, short, int, long) Integer in a decimal form
%e floating point scientific notation of a decimal number
%f floating point decimal number
%g floating point a scientific notation of a decimal number depending on the precision and value.
%h any type Hexadecimal number String of value from hashCode() method.
%n none line separator.
%o integer (bigint, short, int, long, byte) Octal number
%s any type String
%t Date/Time It is the prefix for Date/Time conversions.
%x integer (bigint, short, int, long, byte) Hexadecimal number string.

Example 1:

This is a simple example that explains how the format() method works. Here we have formated a string with a string and an integer. “%s” is a format specifier for a string and “%d” is a format specifier for an integer.

Source code:

public class Example1 {
    
    public static void main(String[] args) {
        String country="India";
        int value=1122;
        String output= String.format("My country is %s", country);
        String output2=String.format("The value is %d", value);
        System.out.println(output);
        System.out.println(output2);
    }
}

Output:

run:
My country is India
The value is 1122
BUILD SUCCESSFUL (total time: 1 second)

Example 2:

The format() method in supports various data types and you can format them into a string type. Here is an example that explains this concept.

Source code:

public class Example2 {
    
    public static void main(String[] args) {  
        String s1 = String.format("%d", 786);           
        String s2 = String.format("%s", "Online tutorial point");  
        String s3 = String.format("%f", 786.21);        
        String s4 = String.format("%x", 987);         
        String s5 = String.format("%c", 'A');       
        System.out.println(s1);  
        System.out.println(s2);  
        System.out.println(s3);  
        System.out.println(s4);  
        System.out.println(s5);  
    }  
}

Output:

run:
786
Online tutorial point
786.210000
3db
A
BUILD SUCCESSFUL (total time: 0 seconds)

Example 3:

In this example, we have used format() method to format the elements of the ArrayList.

Source code:

public class Example3 {

    public static void main(String[] args) {

        obj object1 = new obj(1, "Mukesh", 'B');
        obj object2 = new obj(2, "Sara", 'A');
        obj object3 = new obj(3, "Ali", 'D');
        ArrayList objectList = new ArrayList(Arrays.asList(object1, object2, object3));

        for (int i = 0; i < objectList.size(); i++) {
            System.out.println(String.format("The value is %d", objectList.get(i).val));
            System.out.println(String.format("The name is %s", objectList.get(i).name));
            System.out.println(String.format("The character is %c", objectList.get(i).character));
        }
    }
}

class obj {

    int val;
    String name;
    char character;

    public obj(int val, String name, char character) {
        this.val = val;
        this.name = name;
        this.character = character;
    }
}

Output:

run:
The value is 1
The name is Mukesh
The character is B
The value is 2
The name is Sara
The character is A
The value is 3
The name is Ali
The character is D
BUILD SUCCESSFUL (total time: 0 seconds)

References:

Happy Learning 🙂

Leave A Comment