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

PrintStream.println() is not the same as print(String) followed by println()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8
    • core-libs
    • None
    • Reproduced on JDK 8, 11, and 19.

    • generic
    • generic

      The Javadoc for PrintStream.println(String) says "Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println()."

      (Side note: there's a typo; "terminate" should be plural)

      But that's now how it actually behaves. Instead, it behaves as if it invokes print(String) and then INVOKESPECIAL PrintStream.println().

      Test case:

       import java.io.*;
          public class CrNlTest {
              public static void main(String[] args) throws Exception {
                  try (PrintStream out = new PrintStream(System.out, true) {
                      @Override
                      public void println() {
                          this.print("***");
                          super.println();
                      }
                  }) {
          
                      // Scenario 1
                      out.println("test"); // outputs 74 65 73 74 0a
          
                      // Scenario 2
                      out.print("test"); // outputs 74 65 73 74 2a 2a 2a 0a
                      out.println();
                  }
              }
          }

      Expected output:

      test***
      test***

      Actual output:

      test
      test***

            Unassigned Unassigned
            acobbs Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: