Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does "%1$d" mean in a format string?

I can’t figure out what this %1$d format specifier means. Can someone help me?

int zahl = 100;
System.out.format("A number: %1$d %n", zahl);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1$ represents your first argument which is your zahl variable. %d is the format specifier for integer. The $ convention is used to avoid multiple times your argument is being reused. One example is,

Date date = new Date();
System.out.printf("hours %tH: minutes %tM: seconds %tS%n", date, date, date);

In the above print statement instead of passing your date variable multiple number of times, you can achieve it instead in the following way,

System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading