Java Reference
In-Depth Information
Consider the following snippet of code and its output, which uses relative indexing:
System.out.printf("%1$s, %<s, %<s, %2$s, and %<s", "Ken", "Matt");
Ken, Ken, Ken, Matt, and Matt
This snippet of code uses five format specifiers: "%1$s" , "%<s" , "%<s" , "%2$s" , and "%<s" . It uses two arguments:
"Ken" and "Matt" . Note that it is possible to have less number of arguments than the number of format specifiers if
some format specifiers use relative indexing. The first format specifier of "%1$s" uses explicit indexing to reference the
first argument of "Ken" . The second format specifier of "%<s" uses relative indexing (notice '<' flag) and, therefore,
it will use the same argument, which was used by the previous format specifier, "1$s" . This way, both the first and
the second format specifiers use the first argument of "Ken" . This is confirmed by the output that displays "Ken" as
the first two names. The third format specifier of "%<s" also uses relative indexing. It will use the same argument as
used by the previous format specifier (the second format specifier). Since the second format specifier used the first
argument of "Ken" , the third one will also use the same argument. This is confirmed in the output that shows "Ken"
as the third name. The fourth "%2$s" format specifier uses explicit indexing to use the second argument of "Matt" .
The fifth and the last format specifier of "%<s" uses relative indexing and it will use the same argument that is used by
its previous format specifier (the fourth format specifier). Since the fourth format specifier uses the second argument
of "Matt" , the fifth format specifier will also use the second argument of "Matt" . This is confirmed in the output that
displays "Matt" as the fifth name.
The following statement will throw a java.util.MissingFormatArgumentException because it uses relative
indexing for the first format specifier:
System.out.printf("%<s, %<s, %<s, %2$s, and %<s", "Ken", "Matt");
It is possible to mix all three types of indexing to reference arguments inside different format specifiers in the
same format string. Consider the following statement and its output:
System.out.printf("%1$s, %s, %<s, %s, and %<s", "Ken", "Matt");
Ken, Ken, Ken, Matt, and Matt
The first format specifier uses the explicit indexing to use the first argument of "Ken" . The second and the
fourth format specifiers (both "%s" ) use ordinary indexing. The third and the fifth format specifiers (both "%<s" )
use relative indexing. It is clear from the rule of relative indexing that the third and fifth format specifiers will use the
same arguments as used by the second and the fourth format specifiers, respectively. Which arguments will be used
by the second and the fourth format specifiers? The answer is simple. When you have some format specifiers that
use ordinary indexing and some explicit indexing, just for the purpose of understanding this rule, ignore the format
specifiers that use explicit indexing and number the format specifiers that use ordinary indexing as 1, 2, and so on.
Using this rule, you can think of the above statement the same as the following one:
System.out.printf("%1$s, %1$s , %<s, %2$s , and %<s", "Ken", "Matt");
Notice that you have replaced the first occurrence of "%s" with %1$s" and the second occurrence with "%2$s"
as if they are using explicit indexing. This explains the output generated by the above statement.
 
Search WWH ::




Custom Search