JAVA: Meaning of \n and \t With Code Examples

What does \n mean in JAVA? And what do \t mean in JAVA? In this article, I will show you the difference between \n and \t with various examples. And how you can use it in your JAVA code.

In JAVA, the escape sequences \n and \t are used to print to a new line and provide a space that is equivalent to somewhere 4 to 6 spaces known as tab space.

What is Escape Sequence?

Whenever a character has the precedence of the backslash and is defined in JAVA with a specific meaning, it is considered to be in an escape sequence. The Java escape sequences[1] are listed in the table below.

Escape SequenceDescription
\tInsert a tab in the text at this point.
\bInsert a backspace in the text at this point.
\nInsert a new line in the text at this point.
\rInsert a carriage return in the text at this point.
\fInsert a form feed in the text at this point.
\'Insert a single quote character in the text at this point.
\"Insert a double quote character in the text at this point.
\\Insert a backslash character in the text at this point.

What does \n mean in JAVA?

As previously mentioned in the preceding table, the character \n is used in Java to insert a new line into a text or string at any point in the string. Let’s take a look at some example code for this.

The \n can also be used to break two different sentences into two different lines but using a single print statement as shown in the below example.

public class Solution{
  public static void main(String[] args){
    
    // Testing \n for print new line
    System.out.print("This is the Test String.\nThis is the New Line");

  }
}

Output:

This is the Test String.
This is the New Line

What does \t mean in JAVA?

As already described in the above table, \t is used in JAVA for inserting a tab in the text or string at any given point. Let us see the example code for that.

The \t is equivalent to around 4 spaces to 6 spaces depending upon the application you are using. If you are doing coding in Visual Studio Code then you can change the tab spaces to 4 from 6 if you want as it allows the customization of \t spaces.

public class Solution{
  public static void main(String[] args){
    
    // Testing \t for print new line
    System.out.print("This is the Test String.\tThis line is after tab space");

  }
}

Output:

This is the Test String.	This line is after tab space
JAVA Meaning of \n and \t With Examples and escape sequence

Wrap Up

Other escape sequences, such as those listed in the table above, are also used in JAVA. These escape characters must be used with text, numbers, or strings.

I hope you now have a better understanding of the JAVA escape sequence, how to customize it, and what each escape sequence does to the actual code.

Let me know in the comment section if you have any confusion or if you have any suggestions for the content provided here. I will be happy to change it or help you as soon as possible.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How to Iterate Over Characters of String in JAVA
  2. JAVA String to Int Complete Guide JAVA 8
  3. How To Return Null in Python Discussed
  4. How to Split a String into a List of Words or Letters in Python
  5. Reading A Binary File In Python With Examples