javacross-platformsystem-properties

If we're told to use the system property line.separator instead of hard-coding \n, why does my code work on Windows?


We all read something like this:

use the System.getProperty(String) method to refer to items which depend on the system, such as line terminators and path separators.

The quote is copied from this web site.

I hard-coded an \n, and the code below works on my Windows machine as expected.

package sample;

public class Main {

    public static void main(String[] args) {
        System.out.println("hello\nworld");
    };

}

The output is:

hello
world

I thought the Java compiler replaced \n with \r\n silently. So I downloaded JD GUI, opened the JAR file, and saw Hello\nworld. Can anybody give me an example where \n doesn't work and System.getProperty("line.separator") does?


Solution

  • From the comment: It looks like Command Prompt substitutes the correct line endings, and Notepad doesn't.