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);Ieva surinko 18 taškų.
%sis a placeholder for text.%dis a placeholder for a decimal integer.- The arguments after the template are
vardasandtaskai. - Java inserts the first argument into the first placeholder and the second into the second.
format()returns a newString, saved in the variablezinute.
Method syntax
String.format(String format, Object... args)
String.format(Locale locale, String format, Object... args)
| Parameter | Purpose |
|---|---|
locale | Optional language and region rules for numbers and other localized values. |
format | A template containing ordinary text and format specifiers. |
args | One or more values inserted into the placeholders. |
| Return value | A new formatted String. |
The three dots Object... mean the method accepts a variable number of arguments.
Common format specifiers
| Specifier | Meaning | Example |
|---|---|---|
%s | Text or an object's string representation | "Rūta" |
%d | Decimal integer | 42 |
%f | Floating-point number | 3.500000 |
%b | Boolean value | true |
%c | One character | 'A' |
%% | A literal percent sign | % |
%n | A platform-specific newline | Line break |
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);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.
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);|Duona | 2|
|Obuoliai | 12|
%-10sreserves ten positions for left-aligned text.%4dreserves 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);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);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);Kiekis: 5
Kiekis: 5
| The method | Behavior |
|---|---|
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.