Discussions
Categories
- 197.2K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
What's the Right work of carriage return in java?

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
-
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.
-
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.
Answers
-
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.
-
Thanks a lot brother.I got this.But can you ans me what's this?I got this from an article.
from an article.
-
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.
-
Ok I got it now.Thank you.