Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning \r carriage return and \f form feed in Java?

Tags:

java

What is the meaning of \r carriage return and /f form feed in Java? Can anyone give a small example of when we have to use these types of symbols?

System.out.println("hello world \r rafter \n nafter \f fafter");

I don't know what is what in that above.

The output I got is:

hello world 
 r-after 
 n-after  f-after

I got \r as new line and \n for new line and with \f a box appears for me.

like image 1000
bhv Avatar asked Sep 26 '13 10:09

bhv


3 Answers

You rarely use either of them (in isolation) in modern applications.

\r (carriage return) is what it sounds like if you're familiar with old-fashioned manual typewriters: It moves the "carriage" (the roll the paper is fed through) back to the beginning of the line. On a terminal (or similar), it moves the output point back to the beginning of the line, without moving down a line (usually).

\f is (as you said) a formfeed, it tells old-fashioned printers to start a new page. In computer documents, it's sometimes used to indicate a page break.

Rounding things out, there's also line feed (aka "newline"): \n. This means "move down a line." In some terminals, it just moves down a line without doing a carriage return; on others, it does both.

\n (LF, newline) is the standard text file line break on *nix and derived operating systems. \r\n (CRLF) is the standard text file line break on DOS and Windows. Back in the day, \r (on its own) was the standard line break in text files on the Mac, prior to Mac OS X (which is *nix-derived).

like image 76
T.J. Crowder Avatar answered Sep 28 '22 04:09

T.J. Crowder


To see the difference you must run your code from a command prompt. Eclipse's Console (and similar for other IDEs) does not simulate the behavior of a full TTY terminal and will move to the next line for both \r and \n. On the command line, however, \r will only move the cursor back to the beginning of the current line. This is very convenient when you want to show live progress of your program without scrolling.

The form-feed signal is harder to predict and not as useful. It may have the effect of clearing the terminal window in some cases.

like image 23
Marko Topolnik Avatar answered Sep 28 '22 05:09

Marko Topolnik


Carriage return and form feed are terminal or printer control characters and have no meaning whatsoever in Java.

In a terminal emulation, when you print a \r, the next character will possibly get printed at the first row of the current line (overwriting what is there already).

A form feed, when printed to a terminal may or may not clear the screen. On a printer, it should cause the current page to get ejected, so that printing continues on a new page.

But, as said before, neither the terminal (emulation) nor the printer do care whther the control character was printed by a Java program.

like image 40
Ingo Avatar answered Sep 28 '22 05:09

Ingo