Explain the char data type in java?
A char is a single character in which a letter, a digit, a punctuation mark, a tab, a space or something same. A char literal is a single one character enclosed within single quote marks like this
char myCharacter = 'g';
A few characters are hard to type. For these Java gives escape sequences. This is a backslash followed through an alphanumeric code. For instance '\n'
is the newline character. '\t'
is the tab character. '\\'
is the backslash itself. The subsequent escape sequences are described:
\b
|
backspace
|
\t
|
tab
|
\n
|
linefeed
|
\f
|
formfeed
|
\r
|
carriage return
|
\"
|
double quote, "
|
\'
|
single quote, '
|
\\
|
backslash, \
|
The double quote escape sequence is used majorly inside strings where it would otherwise terminate the string. For example
System.out.println("And then Jim said, \"Who's at the door?\"");
It isn't essential to escape the double quote inside single quotes. The subsequent line is legal in Java
char doublequote = '"';