0

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
techfly
  • 1,826
  • 3
  • 25
  • 31
sandy Roy
  • 5
  • 1
  • 6

2 Answers2

0

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

Radosław Cybulski
  • 2,952
  • 10
  • 21
0

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".

MarianD
  • 13,096
  • 12
  • 42
  • 54