What are all the escape characters?

In Java, an escape character is a character that is preceded by a backslash (\) and is used to represent special characters or character sequences.

Here is a list of all the escape characters in Java:

  • \t: Horizontal tab
  • \n: New line
  • \f: Form feed
  • \r: Carriage return
  • \": Double quote
  • \': Single quote
  • \\: Backslash

Here is an example of how you can use escape characters in a Java string literal:

String str = "Hello\nWorld!";  // This string contains a new line character

In this example, the string "Hello\nWorld!" will be printed as "Hello" on one line and "World!" on the next line.

You can also use escape sequences to represent Unicode characters. For example, to represent the Unicode character U+00A9 (copyright symbol) in a Java string literal, you can use the following escape sequence:

String str = "\u00A9";  // This string contains the copyright symbol

Note that escape characters and escape sequences are only recognized in string literals, and not in other parts of a Java program.