94

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.

Community
  • 1
  • 1
siva636
  • 16,109
  • 23
  • 97
  • 135

8 Answers8

124

\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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
79

\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

airsquared
  • 571
  • 1
  • 8
  • 25
Jules
  • 3,105
  • 2
  • 27
  • 33
17

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

System.getProperty("line.separator")
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
CyrillC
  • 557
  • 1
  • 3
  • 15
  • 1
    what do you mean by "correct result"? it will not be very correct to use that line if you are going to deploy on different OS, because your app will behave different, which may be wrong. For Example if you need a line in a file to end with 0A, and not 0d0a, because a third party protocol requires that - then System.getProperty("line.separator") will break the logic – saferJo Jun 11 '19 at 13:36
7

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.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
6

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.

Dervall
  • 5,736
  • 3
  • 25
  • 48
3

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.

user12208242
  • 315
  • 1
  • 11
2

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.

Lemuel Lee
  • 83
  • 1
  • 8
1

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

Abhishek Raj
  • 109
  • 3
  • 10