Search

Friday, December 14, 2018

Character Escape Sequences and String Literals

Enclosing character constants in single quotes works for most printing characters, but a few characters, such as the carriage return, pose a special problem when a text editor is used. In addition, certain other characters, such as the single and double quotes, have special meaning in Java, so you can not use them directly. For these reasons, Java provides special escape sequences, sometimes referred to as backslash character constants. These sequences are used in place of the characters that they represent. For example, this assign sch the tab character:
ch = '\t';
The next example assigns a single quote toch:
ch = '\'';
Java supports one other type of literal: the string. A string is a set of characters enclosed by double quotes. For example,
"this is a test"


Escape Sequence          Description
\'                                     Single quote
\"                                    Double quote
\\                                     Backslash
\r                                    Carriage return
\n                                    New line
\f                                     Form feed
\t                                     Horizontal tab
\b                                    Backspace
\ddd                               Octal constant (where ddd is an octal constant)
\u xxxx                           Hexadecimal constant (where xxxx is a hexadecimal constant)


0 comments:

Post a Comment