1. Overview
In this tutorial, We'll learn what does mean \n and \t in java?. \n and \t are called special escape sequence characters.
These are used to format the text data as needed. Compared to \t. \n is much used in java applications.
We'll learn about these two escape characters with examples.
2. What does mean of \N in java?
This means \n is used to add the new line to text at the given point. That means when you are working with the text data and you need to separate the two strings with the new line character then you need to use the \n escape character.
In the below example, we created two strings and invoked the println() method by passing the two strings. Inside, printlin() method, we have added the '\n' character to separate the string 1 and string 2 with the new line.
In the output, \n turns into the new line as a result.
Example 1
package com.javaprogramto.programs.escape; public class EscapeSlashNExample { public static void main(String[] args) { String s1 = "java"; String s2 = "programs"; System.out.println(s1 + "\n" + s2); } }
Output
java programs
Let us write a few more examples on the "\n" escape sequence character.
Example 2
We no need to use always two strings to use \N. Directly \n can be inserted into the middle of a string or at any point of the string.
The point \n appears in the string, from the place the text is moved to the next line.
And also \n can be repeatable as needed. There is no restriction on its usage.
package com.javaprogramto.programs.escape; public class EscapeSlashNExample2 { public static void main(String[] args) { System.out.println("hello world \n welcome"); System.out.println("how \n are \n you \n doing \n ?"); } }
Output
hello world welcome how are you doing ?
You can use System.out.println() method and PrintWriter.println() methods to add the next line at the end of the string given to these methods. No need to add this \n escape character explicitly.
3. What does mean of \T in java?
This means \t is used to add the new tab spaces at the given point in the text content. \t must be used inside the text or concatenate of strings combinations. This can not be used other than string data in java.
The same is applicable to the \n also.
\t result is equivalent to pressing the tab button on the keyboard.
Example 3
In this example, we take two strings and these two strings are separated by \t character using print() method.
package com.javaprogramto.programs.escape; public class EscapeSlashTExample3 { public static void main(String[] args) { String s1 = "java"; String s2 = "programs"; System.out.println(s1 + "\t" + s2); } }
Output
java programs
Few more examples of \t usage within the string contents and repetitive usage.
Example 4
package com.javaprogramto.programs.escape; public class EscapeSlashTExample4 { public static void main(String[] args) { System.out.println("hello world \t welcome"); System.out.println("how\tare\tyou\tdoing\t?"); String str1 = "One"; String str2 = "Two"; String str3 = "Three"; String str = str1 + "\t" + str2 + "\t" + str3; System.out.println(str); } }
Output
hello world welcome how are you doing ? One Two Three
4. Conclusion
In this article, we've seen what is the meaning of \t and \n in java. How to use them with the text values in java with examples?
No comments:
Post a Comment
Please do not add any spam links in the comments section.