Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8080252

java.util.Formatter documentation of %n converter is misleading

XMLWordPrintable

    • b77
    • x86
    • os_x

        FULL PRODUCT VERSION :


        A DESCRIPTION OF THE PROBLEM :
        The documentation of java.util.Formatter states the following about the %n converter:

        Line Separator

        The conversion does not correspond to any argument.
        'n' the platform-specific line separator as returned by System.getProperty("line.separator").

        This is incorrect. It uses `System.lineSeparator()`, which reflects only the *initial* value of the "line.separator" property. Changing the property in runtime does not affect the behavior of the "%n" conversion. (Compare to the behavior of java.nio.file.Files.write(), which is affected by runtime changes in "line.separator").

        The documentation of java.util.Formatter should be changed to match the code.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the test case program on a machine whose line separator is LF (Mac, Linux):



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Output:

        [10]
        [13, 10]
        [13, 10]
        ACTUAL -
        Output:

        [10]
        [13, 10]
        [10]

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.nio.charset.StandardCharsets;
        import java.util.Arrays;
        import java.util.Formatter;

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

                System.out.println(Arrays.toString(System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8)));
                System.setProperty("line.separator", "\r\n");
                System.out.println(Arrays.toString(System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8)));
                Formatter f = new Formatter();
                f.format("%n");
                System.out.println(Arrays.toString(f.out().toString().getBytes(StandardCharsets.UTF_8)));

            }
        }
        ---------- END SOURCE ----------

              sherman Xueming Shen
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: