10

My Professor kept telling us that she wants us to use the System.lineSeparator(); to create an empty new line, she didn't specify any reasons, but basically said it was cleaner. What's the difference and should I use it in the described manner over.

System.out.println("[your message]\n")

in contrast to

System.out.println("[your message]");
System.lineSeparator();
Ram
  • 3,887
  • 4
  • 27
  • 49
blkpingu
  • 1,556
  • 1
  • 18
  • 41
  • 1
    The line separator isn't always `\n`, e.g. on windows it is `\r\n`. But aside from that, `System.lineSeparator();` does absolutely nothing. – Andy Turner Mar 17 '18 at 18:47
  • 4
    "My Professor kept telling us she want us to use the System.lineSeparator()" wait, what? This is terrible practice. Are you actually sure she meant that? Use `System.out.println`, or, if you're using `String.format` etc, use `%n`. – Andy Turner Mar 17 '18 at 18:49
  • Google: eol character – Rui Lima Mar 17 '18 at 18:51
  • 3
    @BlkPengu you shoul use `System.lineSeparator()` like that: `System.out.println("[your message]" + System.lineSeparator());` – Artem Petrov Mar 17 '18 at 18:51
  • @Artem no, really, don't. Aside from the verbosity, that creates a new string. – Andy Turner Mar 17 '18 at 18:52
  • 2
    @AndyTurner All I mean is that invoking `System.lineSeparator();` without using its result does nothing – Artem Petrov Mar 17 '18 at 18:53
  • 1
    @Artem right, but the correct way is to use `System.out.println();`, with no parameters, not to use string concatenation. – Andy Turner Mar 17 '18 at 18:55
  • For the record: you are expected to do *serious* research prior posting questions here. Have you tried, well, just putting your question into google first? – GhostCat Mar 17 '18 at 19:19
  • @GhostCat I did and it did yield similar questions. However they where badly discussed and did neither yield the specifics I needed, nor did they include the same details from the questions point of view. I'd be happy to discuss weather or not this is a duplicate and reject the notion I would post here without a reasonable discrepancy in other sources. Thank you for pointing it out. I too think the community rules are of major importance. – blkpingu Mar 17 '18 at 19:35
  • @ArtemPetrov please write this as an answer so I can give you credit – blkpingu Mar 17 '18 at 19:39

3 Answers3

11

The \n character is a single character which is called "new line", usually. Sometimes it may be called "linefeed" to be more accurate. The line separator varies from computer to computer, from Operating System to Operating System. You will get different line separators on Windows and Unix.

If you run this: System.getProperty("line.separator") it will return the line separator that is specific to your OS. On windows it will return \r\n and on Unix it will return \n.

I hope that it will be clear enough for you.

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
  • This is a good explanation. It does not, however, address the terrible advice OP's teacher is f giving – Mad Physicist Mar 17 '18 at 19:07
  • 1
    @MadPhysicist The advice is not terrible. I just think OP misunderstood it. When creating `String`s with newline inside, one shouldn't use `\n` but the platform independent solution instead: `String text = "hello" + System.lineSeparator() + "test";`. – Zabuzard Mar 17 '18 at 19:12
  • 1
    @Zabuza. Possibly. In fact probably. Still, all we have is OP's word. – Mad Physicist Mar 17 '18 at 22:35
7

My Professor kept telling us she want us to use the System.lineSeparator(); to create an emphty new line

Are you actually sure she said that? Because invoking System.lineSeparator(); does nothing: it returns a string, which is simply discarded.

If you want to insert a new line (in System.out), use:

System.out.println();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 2
    I think the teacher wanted to say "*use the result of the method*" and OP just misunderstood. Like `String newline = System.lineSeparator();` and then `String text = "hello" + newline + "test";` etc. – Zabuzard Mar 17 '18 at 19:10
2

Here's what the API documentation has to say about System.lineSeparator:

Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.

On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".

Community
  • 1
  • 1
Chris Martin
  • 30,334
  • 10
  • 78
  • 137