0

in java, we know \r is for carriage return but it gives me a new line!! anyone know why? for example:

public class Hello 
{

    public static void main(String[] args) {

         System.out.println("HELLO\r world");

    }
}

the output is:

HELLO
 world
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Asma Khalaf
  • 1
  • 1
  • 1

2 Answers2

1

Ancient history

The carriage return is a relic of the times of old where e.g. dot matrix printers required one command to advance to a new line and a separate command to return the printer head to the beginning of the line-- these two commands are not necessarily always done at the same time, seeing as you might want to e.g. make a vertical line of some kind on the paper. This behavior maps quite well to the way console output was (and still sometimes is) designated in lines and columns: For example, Emacs is often configured to display the cursor position as rownum,colnum (see related answer on superuser).

Current behavior

Unfortunately, I couldn't find any formal definition of how Java's PrintStream.println(...) handles these characters (if at all); Therefore, I decided to debug your code to see how at least the Oracle implementation on my system handles these characters. It seems that the "magic" is done (or rather not done) in BufferedWriter.write(String, int off, int len): It seems that every character in the string is simply copied to the OS output stream buffer in a very low-level fashion, without any sort of processing of the lines themselves. Therefore, the actual "appearance" of these characters is dependent on the definition thereof by the console you're displaying the output on.

TL;DR: Java is simply sending the character \r to the output stream and it is being treated by your console as a "proper" newline; It's not Java's fault but rather your console's, and I don't know what console you're using.

Community
  • 1
  • 1
errantlinguist
  • 3,658
  • 4
  • 18
  • 41
0

Your carriage return just put an end to the current line. Therefore what you are writing next will be printed on a new line. By the way, to be correct you should always create a new line by using '\r\n'. So the output of your program seems perfectly correct !

Jean DELI
  • 38
  • 1
  • 7
  • 3
    *By the way, to be correct you should always create a new line by using '\r\n'.* This is a dangerous statement and when interpreted literally is in fact wrong: In order to automatically insert the [line separator configured for the system you're running the program on](https://en.wikipedia.org/wiki/Newline#Representations), it is best to use [`System.lineSeparator()`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#lineSeparator--). – errantlinguist Feb 20 '16 at 10:56
  • oh thank you, but the teacher asks us why it doesn't work in the program what the reason? – Asma Khalaf Feb 20 '16 at 11:28
  • 1
    Did your teacher really ask "why it doesn't work"?-- That is extremely vague and misleading because the program in fact does work fine. However, in most cases I would recommend against posting homework questions: If that's really all your teacher asked, your question is fine, but if they asked "what is the behavior of the carriage return here", you're not learning anything by posting that exact question on SO verbatim. – errantlinguist Feb 20 '16 at 11:52