Java - char escape sequence

What are char escape sequence?

A character escape sequence starts with a backslash followed by a character enclosed in single quotes.

There are eight predefined character escape sequences.

Character Escape SequenceDescription
'\n'A linefeed
'\r'A carriage return
'\f'A form feed
'\b'A backspace
'\t'A tab
'\\'A backslash
'\"'A double quote
'\''A single quote

You cannot define your own character escape sequences.

char c1 = '\a'; // A compile-time error. Invalid character escape sequence 
char c1 = '\n'; // Assigns a linefeed to c1 
char c2 = '\"'; // Assigns double quote to c2 

Demo

public class Main {
  public static void main(String[] args) {
    char c1 = '\\'; // Assigns a \ to c1 
    char c2 = '\"'; // Assigns double quote to c2 
    System.out.println(c1);//from www .ja  va2  s . c o  m
    System.out.println(c2);

  }
}

Result