Why carriage return hide the preceded character "l" in my below code.
System.out.println("\tHello \tWor\nl\rd");
Here is the output:
Hello Wor
d
Why carriage return hide the preceded character "l" in my below code.
System.out.println("\tHello \tWor\nl\rd");
Here is the output:
Hello Wor
d
Its not hidden, its overwritten - you've '\n' in a code.
\n is more-or-less enter. Cursor goes to the next line. Then you've l, which is written (on beginning of the line). Then you've \r, which tells console to bring cursor to the beginning of the SAME line (ie where l is already written). Then you write d, which overwrites previous l.
Replace last \r with \n and you'll see: Hello Wor l d
Printing the end part of your string
Wor\nl\rd
means:
print W
, o
, r
, then go to start of the next line (\n
), then
print l
,
then go to start of the same line (\r
) and
print d
(thus overwritting l
).
\r
is return or <CR>
(carriage return, 0x0D
), while \n
means new line or <LF>
(line feed, 0x0A
).
These terms come from typewriters - at the end of the line it was need to return the carriage (with a piece of paper) to the right and then perform "line feed".