How does Java process the backspace terminal control character?


The backspace terminal control character is a special character represented by the ‘\b’notation. It is used to move the cursor one character back. It comes under Java escape characters, these characters are used with backslash (\) and hold a special meaning to the compiler. In this article, we will understand and see the practical implementation of‘\b’ notation through Java example programs.

Working of Backspace Terminal Control Character

Two types of situations may arise while working with this escape character. First, when we hard code the backspace character into a String and the second, when we take input using a keyboard.

Talking about the first situation, Java does not handle the backspace terminal control character by itself. It only writes the byte value of the character to the output stream and then, it depends on the terminal or the console how to interpret it. Some terminals may erase the previous character, while others may display a weird symbol or a few may ignore it.

Let’s understand the previous points using an example

Example

public class BackspaceExample {
   public static void main(String[] args) {
      System.out.println("Tutorialss\bPoint");
   }
}

Output 1

TutorialssPoint

But, when the above code was compiled and executed on the VS Code, it produced the below result.

Output 2

TutorialsPoint

We can see that both compilers produced different results. Hence, it is proved that the backspace terminal control character is interpreted according to the terminal on which it is being compiled.

When it comes to reading user input using the ‘Scanner’ class or any other input methods, Java treats the backspace character as a regular character. It does not automatically interpret it as a special operation. In other words, when the backspace notation is used, the console will display the backspace character as it is and will not remove the previous character from the input.

Let’s take an example where we will take input from users for better understanding

Example

import java.util.Scanner;
public class Input {
   public static void main(String []args) {
      Scanner read = new Scanner(System.in);
      System.out.println("Enter a String: ");
      while(read.hasNext()) { // runs till the last character
         String st = read.nextLine();
         System.out.println("String value: " + st);
      }
   }
}

Output

Enter a String:
Shriansh\bKumar
String value: Shriansh\bKumar

As explained earlier, putting backspace character into user-input values will not be considered as a special operation. We can see in the output that the ‘\b’ character is printed as it is. In the above code, we have created an instance of class ‘Scanner’ and using a while loop we have read and write the input provided by a user.

Conclusion

This article concludes that we cannot rely on the backspace terminal control character to erase any characters of a string in Java as it may work differently depending on the terminals or compilers. Also, Java treats the backspace character as a regular character when it comes to user inputs. If we want to remove characters from a string in Java, we should use other means such as ‘substring’, ‘replace’ or ‘StringBuilder’ and ‘StringBuffer’.

Updated on: 20-Jul-2023

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements