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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Return value of Java String format()

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

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more