What Are the Differences Between Char Literals '\N' and '\R' in Java

What is the difference between \r and \n?

They're different characters. \r is carriage return, and \n is line feed.

On "old" printers, \r sent the print head back to the start of the line, and \n advanced the paper by one line. Both were therefore necessary to start printing on the next line.

Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r to move to the start of the line and overwrite the existing text.

More importantly, Unix tends to use \n as a line separator; Windows tends to use \r\n as a line separator and Macs (up to OS 9) used to use \r as the line separator. (Mac OS X is Unix-y, so uses \n instead; there may be some compatibility situations where \r is used instead though.)

For more information, see the Wikipedia newline article.

EDIT: This is language-sensitive. In C# and Java, for example, \n always means Unicode U+000A, which is defined as line feed. In C and C++ the water is somewhat muddier, as the meaning is platform-specific. See comments for details.

Difference between \n and \r?

In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).

But seriously, there are many:

  • in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), \r was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, \r\n, in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows), \r\n is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like "terminals", \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) -- that's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-) Wikipedia has a more detailed explanation.
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)

In practice, in the modern context of writing to a text file, you should always use \n (the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).

What's the difference between \n and \r\n?

\r\n is a Windows Style

\n is a POSIX Style

\r is a old pre-OS X Macs Style, Modern Mac's using POSIX Style.


\r is a carriage return and \n is a line feed, in old computers where it not have monitor, have only printer to get out programs result to user, if you want get printing from staring of new line from left, you must get \n for Line Feed, and \r for get Carriage return to the most left position, this is from old computers syntax saved to this time on Windows platform.

What is the difference between \r and \n?

Because it just isn't.

\r means "carriage return".
\n means "new line" line feed

Some systems are wise enough to suggest that all they need to get the cursor to a new line is \n. Others think that it's more appropriate to go \r\n, because, they may claim that the column position needs to be reset as well (i.e. the \r).

Sometimes in computing things are 'just this way'. This is one of those cases :)

Difference between \r and \n

\r is "Carriage Return" (CR, ASCII character 13), \n is "Line Feed" (LF, ASCII character 10). Back in the days, you had two ASCII characters at the end of each line to tell a printer what to do - CR would tell the printer to go back to the left edge of the paper, LF would advance to the next line.

Operating systems still have different conventions as to what the end of a line looks like -- some of them have \n\r, some have \n, some have \r\n.

In Javascript, you mostly deal with \n - this is how strings are typically switching to the next line. However, depending on what strings you are working with, you may be encountering \r as well.

difference between newLine() and carriage return(\r)

Assuming you mean this:

public static String newLine = System.getProperty("line.separator");

newLine is environment agnostic \r isn't.

So newLine will give you \r\n on windows but \n on another environment.

However, you shouldn't use this in a JTextArea and println will work fine with just \n on windows.

Edit now that I've seen the code and your comment

In your situation. I think you should use your own constant - \r\n

  File f = new File(strFileGenLoc);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
rs = stmt.executeQuery("select * from jpdata");
while ( rs.next() ) {
bw.write(rs.getString(1)==null? "":rs.getString(1));
bw.write("\\r\\n");
}

What is the difference between a line feed and a carriage return?

A line feed means moving one line forward. The code is \n.
A carriage return means moving the cursor to the beginning of the line. The code is \r.

Windows editors often still use the combination of both as \r\n in text files. Unix uses mostly only the \n.

The separation comes from typewriter times, when you turned the wheel to move the paper to change the line and moved the carriage to restart typing on the beginning of a line. This was two steps.



Related Topics



Leave a reply



Submit