Forum Stats

  • 3,770,408 Users
  • 2,253,104 Discussions
  • 7,875,445 Comments

Discussions

What's the Right work of carriage return in java?

User_XK48E
User_XK48E Member Posts: 3 Green Ribbon

I having this problem when I use \r carriage return? Is anyone  can help me to find out where the problem is? I am beginner e.g I am learning java. I would be very greatful If anyone help me!!

Best Answers

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee
    Accepted Answer

    There are two codes that are involved in denoting new lines of text, known as "Carriage Return" "\r" and "Line Feed" "\n" collectively known as CRLF.

    • All Unix, Linux and MacOS systems use "\n" 
    • Windows is using "\r\n" 
    • Few antiquated platforms used "\r"

    Java provides a universal capability to get the current platform line separator: System.lineSeparator()

    Also, you can use "%n" as a new line separator within printf method provided by PrintStream and PrintWriter, and String.format method.

    User_XK48E
  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee
    Accepted Answer

    Indeed, the original purpose of the "\r" is to move the cursor to the start of the current line. - Think printer codes, because that's where these control symbols originate from. In the olden days before computers started using monitors widely, printing was the main form of the output, and the behaviour of the "\r" was indeed as described in the quoted article. Therefore, any text after this symbol would simply be printed on top of whatever characters were on this line. However, the behaviour that is described in the article you've quoted is not entirely correct, since the actual result would be system specific, because two reasons: First is the the difference between Unix and Windows in the way the "\r" is interpreted, i.e. Windows uses "\r\n" as a line terminator. The second reason is that you are not actually using a printer, but rather directing the output to the screen, which does not behave in the same way, i.e. it does not print text character-by-character, then return to the start of the line and print on top. Instead it can simply ignore whatever text was on this line before the "\r" symbol and only output what comes after this on the given line. This would explain why you only see the text that comes after the "\r" symbol.

    P.S. I must say this question seems to be of more of an academic, rather than a practical interest. Because practically you just use the System.lineSeparator() which does the trick for any platform.

    User_XK48E

Answers

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee
    Accepted Answer

    There are two codes that are involved in denoting new lines of text, known as "Carriage Return" "\r" and "Line Feed" "\n" collectively known as CRLF.

    • All Unix, Linux and MacOS systems use "\n" 
    • Windows is using "\r\n" 
    • Few antiquated platforms used "\r"

    Java provides a universal capability to get the current platform line separator: System.lineSeparator()

    Also, you can use "%n" as a new line separator within printf method provided by PrintStream and PrintWriter, and String.format method.

    User_XK48E
  • User_XK48E
    User_XK48E Member Posts: 3 Green Ribbon

    Thanks a lot brother.I got this.But can you ans me what's this?I got this from an article.

    from an article.

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee
    Accepted Answer

    Indeed, the original purpose of the "\r" is to move the cursor to the start of the current line. - Think printer codes, because that's where these control symbols originate from. In the olden days before computers started using monitors widely, printing was the main form of the output, and the behaviour of the "\r" was indeed as described in the quoted article. Therefore, any text after this symbol would simply be printed on top of whatever characters were on this line. However, the behaviour that is described in the article you've quoted is not entirely correct, since the actual result would be system specific, because two reasons: First is the the difference between Unix and Windows in the way the "\r" is interpreted, i.e. Windows uses "\r\n" as a line terminator. The second reason is that you are not actually using a printer, but rather directing the output to the screen, which does not behave in the same way, i.e. it does not print text character-by-character, then return to the start of the line and print on top. Instead it can simply ignore whatever text was on this line before the "\r" symbol and only output what comes after this on the given line. This would explain why you only see the text that comes after the "\r" symbol.

    P.S. I must say this question seems to be of more of an academic, rather than a practical interest. Because practically you just use the System.lineSeparator() which does the trick for any platform.

    User_XK48E
  • User_XK48E
    User_XK48E Member Posts: 3 Green Ribbon

    Ok I got it now.Thank you.