What does \n and \t mean in Java with examples

In this post, I will be sharing what does \n and \t mean in Java with examples. Both \n and \t are called escape sequences in java.

Read Also: String Interview Questions in Java

What does \n mean in Java?

This means to insert a new line at this specific point in the text. In the below example, "\n" is used inside the print statement, which indicates that the control is passed to the next line. As a result, the text following "\n" will be printed on the next line.

public class NewLineExample {
    public static void main(String args[]) {
      String str1 = "Hello";
      String str2 = "world";
      System.out.print(str1 + "\n" + str2);
    }
}


Output:
Hello
world

What does \t mean in Java?

This means to insert a new tab at this specific point in the text. In the below example, "\t" is used inside the println statement. It is similar to pressing the tab on our keyboard.

public class NewTabExample {
    public static void main(String args[]) {
          String str1 = "Hello";
          String str2 = "world";
          System.out.println(str1 + "\t" + str2);
    }
}


Output:
Hello     world

That's all for today, please mention in the comments in case you have any questions related to what does \n and \t mean in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry