Java - What is the output: Relative Indexing in a Format Specifier?

Question

What is the output of the following code?

System.out.printf("%<s, %<s, %<s, %2$s, and %<s", "Java", "Javascript");


Click to view the answer

java.util.MissingFormatArgumentException

Note

The statement will throw a java.util.MissingFormatArgumentException because it uses relative indexing for the first format specifier:

Demo

public class Main {
  public static void main(String[] args) {

    System.out.printf("%<s, %<s, %<s, %2$s, and %<s", "Java", "Javascript");
  }// w w w  .ja v  a 2  s .  co  m

}

Result