JAVA: Meaning of \n and \t With 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.

\n and \t are the escapes sequences in JAVA that are used for printing to the new line and providing a space equivalent to tab.

What is Escape Sequence?

An escape sequence in JAVA means whenever a character is having precedence of backslash and are defined in JAVA with some specific meaning. The below table shows the Java escape sequences.[1]

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 already described in the above table, \n is used in JAVA for inserting a new line in the text or string at any given point. Let us see the example code for that.

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, \n 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.

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

Similarly, other escape sequences that are listed in the above table are used in JAVA. You need to use these escapes characters with text or numbers or string.

So that’s it for today. If you have any suggestions or query feel free to comment. Also, like my Facebook page and follow me on Twitter.

Share your love

2 Comments

Leave a Reply