Escape Characters
Escape Characters
Try running the code given below in your java compiler.
As we can see that the code gives an error. This is because the compiler assumes that the string ends after the 2nd quotation mark.
This can be solved by using ‘\’ (backslash). Backslash acts as an escape character allowing us to use quotation marks in strings.
Example:
Output:
Similarly to use a backslash in the string we must escape it with another backslash.
Example:
Output:
We also have an escape character for printing on a new line(\n), inserting a tab(\t), backspacing(\b), etc.
Example:
Output: