[Java Basics] The difference between \n, \t, \r

\n: In Windows, it means line break, to the beginning of the next line. In Linux and Unix, it means that the left and right positions do not change, change to the next line, and the ordinates before and after the line break are the same.

\t: Represents indentation in Window, which is equivalent to the distance between pressing the Tab key, generally eight characters.

\r: In Window, it means carriage return, that is, move to the beginning of the line.

 

        There must be many programmers who have encountered escape characters related problems when learning Java. I was also very confused by \n,\r,\t recently. After reading many CSDN articles, I found myself I didn’t figure it out, but I became more confused. One of the reasons is that many bloggers’ explanations in the article did not specify what system the escape character applies to.

test:

public class test {
public static void main(String[] args) {
	System.out.print("2021\n2022");
	System.out.print("2023\r2024\t2025");
}
}

Output result:

        From the result, \n, \t is not different from the previous description, but the result of \r is not the same as expected. The effect is also to wrap to the beginning of the next line, which has the same effect as \n . As for why the result does not match the definition, I am also very confused. I hope some big guys can explain it in the comments.

      

The origins and differences of the concepts of "Carriage Return" and "Line Feed".

       Before the computer appeared, there was a gadget called Teletype Model 33 (Teletype Model 33, the concept of tty under Linux/Unix also comes from this), which can type 10 characters per second. But it has a problem, that is, it takes 0.2 seconds to type a line and wrap it, which is exactly two characters. If a new character is passed in within 0.2 seconds, then this character will be lost. Therefore, the developers thought of a way to solve this problem, which is to add two characters at the end of each line. One is called "return", which tells the typewriter to position the print head on the left margin; the other is called "newline", which tells the typewriter to move the paper down one line. This is the origin of "line feed" and "carriage return", as can be seen from their English names.

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/113577688