Java - What is the output: width and .precision?


What is the output of the following code?

Consider the following snippet of code:

System.out.printf("'%1.4s'", "Java");

Click to view the answer



The argument value is "Java" and the format specifier is "%1.4s" where 1 is the width and 4 is the precision.

Because the precision value of 4 is equal to the length of the argument, which is 4, there is no effect of the precision.

Because the width value of 1 is less than the width of the result after precision is applied, there is no effect of the width value on the output.


public class Main {
  public static void main(String[] args) {
    System.out.printf("'%1.4s'", "Java");
  }/*from w  w  w. j av a2 s . c o m*/