Do I have to specify a variable for each identical argument in String.format?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, you do not need to pass the same value multiple times to String.format just because it appears more than once in the output. The format syntax supports argument indexes, which let you reuse an earlier argument anywhere in the format string.
The Default Behavior
Without indexes, String.format consumes arguments in order from left to right. That means repeated placeholders normally require repeated arguments.
This works, but it is repetitive and gets harder to maintain when the format string is long.
Reuse an Argument With an Index
Java format specifiers can include an explicit argument position. The syntax 1$ means "use the first argument again", 2$ means the second, and so on.
That is the usual answer to this question: pass the value once, then refer to it multiple times with the same argument index.
Why Argument Indexes Are Useful
Indexes are not only for repeated values. They are also useful when you need to reorder values in the output, which is common in localization and templates where word order changes by language.
Because the format string controls the positions explicitly, the caller can pass the values once and the formatter can reuse or reorder them as needed.
Keep Types Matched to the Placeholder
The argument index only selects the argument. The conversion part of the specifier still matters. For example:
- '
%1$streats the first argument as a string representation' - '
%1$dexpects the first argument to be an integer' - '
%1$.2fformats the first argument as a floating-point number with two decimals'
The same argument can be reused as long as the conversion makes sense for the underlying type.
A Practical Example
Consider building an SQL debug message, a file name, or a repeated label:
Passing table once is cleaner than repeating it three times in the argument list. It also reduces the chance of accidentally changing one copy and forgetting the others.
When Repetition Is Still Reasonable
If the format string is tiny, repeating the argument may not be a real problem. The indexed form becomes most valuable when:
- the same argument appears many times
- the template is long
- output order is not the same as input order
- the string is used in localization
In short, indexes are a readability tool as much as a convenience feature.
Common Pitfalls
The first mistake is forgetting the $ syntax. %1s is not the same as %1$s; the indexed form must include the dollar sign after the position.
Another common issue is mixing indexed and non-indexed placeholders in the same format string. That can lead to confusing behavior and is best avoided.
Type mismatches are another source of errors. Reusing %1$d is fine for integers, but it will fail if the first argument is actually a string.
Finally, do not overuse formatting for cases where normal string concatenation or text blocks would be clearer. String.format is most helpful when you truly need structured placeholder substitution.
Summary
- You do not need to pass the same Java argument multiple times to
String.format. - Use indexed placeholders such as
%1$sto reuse the first argument. - Argument indexes also help when values must appear in a different order.
- The conversion letter still has to match the argument type.
- Avoid mixing indexed and non-indexed placeholders in one format string.

