Tab in Java

Rupam Yadav Feb 01, 2023 Dec 29, 2021
  1. Using \t Tab Escape Sequence Character in Java
  2. Unicode Character Tabulation in Java
Tab in Java

When a character in Java appears after the \ backlash, it is referred to as Java Escape Characters or Escape Sequence. This article will teach more about the \t Escape Sequence.

Using \t Tab Escape Sequence Character in Java

Java Escape Sequences are valid character literal used to perform a specific task. The Escape Sequence, \t is used for tab space.

In other words, \t inserts a tab. We use Escape Sequence when we need to format a String.

If we use \t at a specific point in a string, it will insert a new tab at that point. The Escape sequence for tab, \t can be used inside a print statement, as given in the code below.

The String to be formatted is placed inside the double quotes. The Escape Sequence for tab \t is placed between the words inside the "".

As we can see in the output, it inserts a tab between the two words. We can format the string and insert a tab at any point in the text.

public class Test {
    public static void main(String[] args) {
        System.out.println("Happy\tCoding");
    }
}

Output:

Happy	Coding

Unicode Character Tabulation in Java

We can also use the Unicode Character U+0009 to insert a tab at a given point in the text to format it. Unicode is a standard to encode text representing almost every character of well-known languages globally.

It is a 16-bit character encoding standard. The Unicode that represents tab space is U+0009.

Here, in the program shown below, we have taken a String type variable tab and initialized it a value "\u0009". It inserts a horizontal tab.

With a tab space between these two strings, we want to print HelloEveryone. To do this, we used the + operator to concatenate two strings with the tab variable.

We pass the strings inside the print function. The tab variable will format the string and print it as an output.

We can see, the Unicode character inserts a tab space between the two given strings. We can see the output string is printed with a tab in place of the tab variable.

public class Test {
    public static void main(String[] args) {
        String tab = "\u0009";
        System.out.println("Hello"+tab+"Everyone");
    }
}

Output:

Hello	Everyone
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn