Joke Collection Website - Public benefit messages - Carriage return in java

Carriage return in java

\r' is carriage return,' \n' is line feed, the former moves the cursor to the beginning of the line, and the latter moves the cursor down by one space. The commonly used Enter is the sum of two. Let's read an article.

Carriage return and line feed

Today, I finally understand the origin and difference between "carriage return" and "line feed".

Before the advent of computers, there was something called Model 33 Teletypewriter, which could type 10 characters per second. But it has a problem, that is, it takes 0.2 seconds to type a new line, just two characters. If a new character appears within 0.2 seconds, it will be lost.

So, the developer thought of a way to solve this problem, that is, adding two characters after each line to indicate the end. One is called "Enter", which tells the typewriter to position the print head at the left margin; The other is called "line feed", which tells the typewriter to move the paper down one line.

This is the origin of "line feed" and "carriage return", and we can also see a thing or two from their English names.

Later, the computer was invented and these two concepts were extended to computers. At that time, memory was very expensive, and some scientists thought it was too wasteful to add two words at the end of each line. Just add one. As a result, there was disagreement.

In Unix system, there is only "",namely "\ n ";; In the Windows system, each line is marked with "< return to train >", that is, "\ r \ n ";; In the Mac system, every line ends with "". A direct consequence is that if you open a file under Unix/Mac system in Windows, all the words will become one line; When opening a file in Windows under Unix/Mac, there may be an M symbol at the end of each line.

C language programming (windows system) \r is to return to the beginning of this line, which will overwrite the output of the previous line.

For example:

int main() {

Cout & lt& lt hahaha "<& lt" \ r"<& lt Xixi ";

}

Finally, only Xixi is displayed, covering hahaha.

\n is carriage return+line feed. Move the cursor to the beginning of the line, and then move to the next line, that is, the beginning of the next line.

int main() {

Cout & lt& lt hahaha "<& lt" \ n"<& lt Xixi ";

}

Then display

hahaha

I summarized it myself.

/t is equivalent to the Tab key of the keyboard, /n new line, /r enter, and the basic concept should be that enter means to go back to the front, and line feed means to change a line.

Different operating systems have different line breaks:

/r Mac

/n Unix/Linux

/r/n Windows