What are the differences between char literals '\n' and '\r' in Java?

JavaNewlineCarriage Return

Java Problem Overview


In Java, the newline and carriage return characters are both seem to be showing same effect.

What are the actual differences between char literals \n and \r in Java?


Note that the above asks about the \n character, not the newLine() function on BufferedWriter, and so this other question isn't relevant.

Java Solutions


Solution 1 - Java

\n is a line feed (LF) character, character code 10. \r is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF). On Unix systems and their derivatives, only LF is used. (Macs prior to Mac OS X used CR, but Mac OS X is a *nix derivative and so uses LF.)

In the old days, LF literally did just a line feed on printers (moving down one line without moving where you are horizonally on the page), and CR similarly moved back to the beginning of the line without moving the paper up, hence some systems (like Windows) sending CR (return to the left-hand side) and LF (and feed the paper up).

Because of all this confusion, some output targets will accept multiple line break sequences, so you could see the same effect from either character depending on what you're outputting to.

Solution 2 - Java

\n is for unix
\r is for mac (before OS X)
\r\n is for windows format

you can also take System.getProperty("line.separator")

it will give you the appropriate to your OS

Solution 3 - Java

It depends on which Platform you work. To get the correct result use -

System.getProperty("line.separator")

Solution 4 - Java

The difference is not Java-specific, but platform specific. Historically UNIX-like OSes have used \n as newline character, some other deprecated OSes have used \r and Windows OSes have employed \r\n.

Solution 5 - Java

It actually depends on what is being used to print the result. Usually, the result is the same, just as you say -

Historically carriage return is supposed to do about what the home button does: return the caret to the start of the line.

\n is supposed to give you a new line but not move the caret.

If you think about old printers, you're pretty much thinking how the original authors of the character sets were thinking. It's a different operation moving the paper feeder and moving the caret. These two characters express that difference.

Solution 6 - Java

When you print a string in console(Eclipse),\n,\r and \r\n have the same effect,all of them will give you a new line;but \n\r(also \n\n,\r\r) will give you two new lines;when you write a string to a file,only \r\n can give you a new line.

Solution 7 - Java

The above answers provided are perfect. The LF(\n), CR(\r) and CRLF(\r\n) characters are platform dependent. However, the interpretation for these characters is not only defined by the platforms but also the console that you are using. In Intellij console (Windows), this \r character in this statement System.out.print("Happ\ry"); produces the output y. But, if you use the terminal (Windows), you will get yapp as the output.

Solution 8 - Java

On the command line, \r will move the cursor back to the beginning of the current line. To see the difference you must run your code from a command prompt. Eclipse's console show similar output for both the expression. For complete list of escape sequences, click here <https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsiva636View Question on Stackoverflow
Solution 1 - JavaT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavaJulesView Answer on Stackoverflow
Solution 3 - JavaCyrillCView Answer on Stackoverflow
Solution 4 - JavaUku LoskitView Answer on Stackoverflow
Solution 5 - JavaDervallView Answer on Stackoverflow
Solution 6 - JavaLemuel LeeView Answer on Stackoverflow
Solution 7 - Javauser12208242View Answer on Stackoverflow
Solution 8 - JavaAbhishek RajView Answer on Stackoverflow