STRING STATIC METHODS

Java String format() method

String.format() creates formatted text from a template. The template marks where Java inserts strings, numbers and other values.

Your first formatted string

String vardas = "Ieva";
int taskai = 18;

String zinute = String.format("%s surinko %d taškų.", vardas, taskai);

System.out.println(zinute);
Console output

Ieva surinko 18 taškų.

  1. %s is a placeholder for text.
  2. %d is a placeholder for a decimal integer.
  3. The arguments after the template are vardas and taskai.
  4. Java inserts the first argument into the first placeholder and the second into the second.
  5. format() returns a new String, saved in the variable zinute.

Method syntax

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

String.format(Locale locale, String format, Object... args)

String.format() parameters
ParameterPurpose
localeOptional language and region rules for numbers and other localized values.
formatA template containing ordinary text and format specifiers.
argsOne or more values inserted into the placeholders.
Return valueA new formatted String.

The three dots Object... mean the method accepts a variable number of arguments.

Common format specifiers

Common String.format() specifiers
SpecifierMeaningExample
%sText or an object's string representation"Rūta"
%dDecimal integer42
%fFloating-point number3.500000
%bBoolean valuetrue
%cOne character'A'
%%A literal percent sign%
%nA platform-specific newlineLine break
The specifier must match the argument.

For example, passing text where the template expects %dcauses a runtime IllegalFormatConversionException.

Decimal places

import java.util.Locale;

double kaina = 3.5;
String tekstas = String.format(Locale.US, "Kaina: %.2f Eur", kaina);

System.out.println(tekstas);
Console output

Kaina: 3.50 Eur

Specifier %.2f means a floating-point number with two decimal places. Locale.US explicitly selects a decimal point in this teaching example.

Locale can change the result.

With the default Lithuanian locale, the same value may appear as 3,50. This is often desirable in user-facing text.

Width and alignment

String pirma = String.format("|%-10s|%4d|", "Duona", 2);
String antra = String.format("|%-10s|%4d|", "Obuoliai", 12);

System.out.println(pirma);
System.out.println(antra);
Console output

|Duona     |   2|
|Obuoliai   |  12|

  • %-10s reserves ten positions for left-aligned text.
  • %4d reserves four positions for a right-aligned number.
  • The vertical bars are not format specifiers: they make the field boundaries easier to see.

A percent sign and newline

String tekstas = String.format("Nuolaida: 20%%%nLiko: %d vnt.", 4);

System.out.print(tekstas);
Console output

Nuolaida: 20%
Liko: 4 vnt.

Since a single % starts a format specifier, a literal percent sign uses %%. The specifier %n inserts the newline sequence appropriate for the operating system.

format() prints nothing by itself

String zinute = String.format("Labas, %s!", "Rūta");

// format() only created the string; print it separately
System.out.println(zinute);
Console output

Labas, Rūta!

String.format() only creates and returns text. Save the result, pass it to a method, return it from a method or print it later.

String.format() and printf()

String tekstas = String.format("Kiekis: %d", 5);
System.out.println(tekstas);

System.out.printf("Kiekis: %d%n", 5);
Console output

Kiekis: 5
Kiekis: 5

The difference between format() and printf()
The methodBehavior
String.format()Returns a new Stringwithout printing it.
System.out.printf()Sends the formatted result directly to the console.

Common mistakes

  • Specifiers and arguments are in different orders.
  • A specifier receives an argument of the wrong type.
  • Forgetting that format() returns a result without displaying it.
  • Writing a percent sign as % instead of %%.
  • Using formatting instead of a precise numeric type for financial calculations: formatting changes presentation, not calculation accuracy.