How does a carriage return (\r) works in Java

Hi coders! today we are going to study a special character carriage return in Java. It is denoted by \r. Special characters are the character sequence that is used to perform a specific task. The main use of this character is to bring the cursor to the starting of the line without changing the line. It remains on the same line. All the text, written after it is put forward at the starting of the same line. It replaces the same number of old characters with new characters. Just like in Newline character (\n)  the cursor is moved to a new line.

\r or carriage return in Java

Let us understand it with the help of an example.
If we write :

System.out.print("Hello \nWorld");



Here all the letters written after \n are put in the next line. Since \n moves the cursor to the next line. Now we will see what happens when we use \r instead of \n.

System.out.print("Hello healthy \rWorld");

then the output will be:

World healthy

Here World which is a 5 letter word replaces the word Hello which is also a 5 letter word. \r does 2 kinds of work. first, it moves the cursor to the starting of the same line, and second, it replaces the same number of alphabet previously written with the same number of alphabets written after \r.

Let us look at another fun example of the above.

System.out.print("Gita is going to Market\rS ");

The output will be:

Sita is going to Market

Here S replaces G because both are of the same length which is 1. similarly a 4-alphabet word will replace Gita in the above example. Its action is similar to that of an old age typewriter. There we have to change it manually. Here we can do it with the help of a simple character sequence.

Hope you understand the topic.

Also read:

How to print Double Quotes In Java

8 responses to “How does a carriage return (\r) works in Java”

  1. monika says:

    not correct what you have been explained here!!

  2. Soumava Chakraborty says:

    Nice article really helped to resolve my query.

  3. Andrew Nyongesa says:

    Excellent information Keep on enlightining us be blessed.

    • santosh says:

      Hi friends,
      \r is the escape sequence .
      (change the data which said above)
      public class Main
      public static void main(String[] args)
      /System.out.println(“Gita is going to Market\rS “);
      System.out.println(“Hello healthy \rWorld”);



      Gita is going to Market
      Hello healthy

  4. SP SONAM says:

    class xyz
    { public static void main(String arg[])
    { System.out.print(“Hello healthy \rWorld”);
    } }
    Hello healthy
    so /r is the Escape sequence that inserts a carriage return or enter in the next line.(AS WE PRESS THE ENTER KEY TO GO TO NEXT LINE.)

    • Wartika says:

      Then what is the difference between \n and \r

      • Yannick Mussche says:

        Hey Wartika

        \n is the command to start printing on another line. Suppose I have this:
        “Hello world I am a programmar”, and I would sout it, I would get: “Hello world I am a programmar”.
        But in some case readability ask to print every word on 1 line. So you would rather create the String like this:
        String text = “Hello \nworld \nI \nam \na \nprogrammar”; If you sout this you will get:

        The return sequence \r only prints the things after the the last return character. For example.
        String text = “Hello \nworld \nI \nam \na \nprogrammar” would print: programmar.

        I also cannot give an example in which case this would be usefull, but that is the difference.

        • Yannick Mussche says:

          To add to my response. In fact it prints like this:
          first it prints Hello. Than it backslahes and prints world, .. untill the final word programmar, which is the only word you see printed out.

Leave a Reply

Your email address will not be published. Required fields are marked *