Java

How to Use \t tab Escape Sequence Character in Java

An escape sequence is a character followed by a backslash (\) mostly utilized to carry out a certain task. Java supports eight escape sequences or characters. When you declare a String in Java, different formats can be applied to it to get the desired String. For example, to print a tab space between two words, you can use the “\t” escape sequence character.

This article will explain the use of \t Java escape sequence characters.

How to Use \t tab Escape Sequence Character in Java?

In Java, the “\t” escape sequence creates a tab space where it is used. It is mostly utilized inside the print statement while formatting a String, where the String that needs to be formatted is enclosed in double quotations (“ “). The tab escape sequence is placed between the words inside the quotations (” “).

Example 1: Add \t tab Escape Sequence Character Within a String

In this example, we will print a String “LinuxHint” with “\t” tab escape sequence:

System.out.println("Linux\tHint");

 

The output shows that we have successfully placed a tab space between the two words “Linux” and “Hint”.

Example 2: Concatenate \t tab Escape Sequence as a Character With String

You can also concatenate the escape sequence “\t” as the character with String. It will also perform the same operation:

System.out.println("LinuxHint" + '\t' + "world");

 

Output

Example 3: Use Unicode for \t tab Escape Sequence

There is one more method to use the “\t” tab escape sequence with the Unicode character “u0009”. Every single character of well-known languages is practically represented by the universal text encoding standard known as Unicode:

System.out.println("LinuxHint" + "\u0009" + "world");

 

The output proves that the Unicode with a backslash “\u0009” also placed a tab space between the two words “LinuxHint” and “world”:

We have provided all the instructions related to the use of the \t escape sequence in Java.

Conclusion

The “\t” escape sequence is used to place a tab space between words. It can be utilized as an escape sequence character between the String inside a print statement or with the Unicode character “\u0009” of a tab. A tab can also be placed by concatenating it with the String. In this article, we explained the use of the escape sequence in Java with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.