Java String format()

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In this article, we'll have an introduction to the format() function for a String data type in Java programming language. The Java String format() method is used to format String class objects according to a certain format or locale.

Syntax and parameters of Java String format() function

The format function returns a String. Two syntaxes are available for using the format() function in Java. They are:

1: format(Locale loc, String format, Object... args)

  • loc is the Locale that needs to be applied by the format function.
  • format is the String object representing the output String format.
  • ...args are zero or more number of arguments that the String object "format" could need.

2: format(String format, Object... args)

  • format is the String object representing the output String format.
  • ...args are zero or more number of arguments that the String object "format" could need.

Return value of Java String format()

The format() function returns as output a String object, which is the formatted String as required.

Example of Java String format()

Output:

In the above example, we obtained str1 and str2. They are String objects that are outputs of the format method. In the first format() call (the result of which is stored in String str1), we place the second argument String "s" at the position of the specifier "%s" in the first argument String literal.

In second format() call (upon str2) we place the float n in the argument String at the position of the specifier "%f".

Exceptions of Java String format()

The Java String format() method could throw one of the following two exceptions:

1: NullPointerException

This exception occurs if the argument format String object is null.

2: IllegalFormatException

This exception occurs if the format argument String is illegal or if there are insufficient arguments.

Output :

Examples for Java String format() Method

1: String formatting of numbers

The Java String format method takes two arguments. The first argument is a String object representing the pattern of how many decimals places we want to get the output. The second argument is the required numeric value.

Output :

2: String formatting with multiple format specifiers

The following example will format a String object using two specifiers.

Output :

3: String formatting of decimal numbers

The following example illustrates the String formatting of a decimal number.

Output :

4: Padding numbers with spaces and zeroes

If we wish to pad a number with zeros or spaces, we may use the String format() method as follows:

Output :

5: Using 0x and 0 before hexadecimal and octal

Output :

The code above converts a number to its hexadecimal form. The number 3 in "%03X" signifies that the width of the hexadecimal number output will be 3. Since, in our case, the output (7B) was shorter (of width 2), an extra zero was prepended to the output String object res to make it of width 3.

Use of Java String format() with locale

As discussed earlier, there are two syntaxes of the format method. So far, we have used only the first syntax. Now, let us have a look at the second syntax, which enables us to use a particular locale.

Output :

The output would vary with the Locale used.

Conclusion

  • The Java String format() method formats String class objects according to a certain format or locale.
  • There are two syntaxes available for using the format() function in Java: format(Locale loc, String format, Object... args) format(String format, Object... args)
  • The format() method returns a String object as output, which is the formatted String as required.
  • The Java String format() method could throw either NullPointerException or IllegalFormatException.