Java Reference
In-Depth Information
In fact, it's not strictly necessary to use an escape sequence to specify a single quote within a string, but
obviously it will be when you want to specify it as a single character. Of course, it is always necessary to
specify a double quote within a string using an escape sequence otherwise it would be interpreted as the
end of the string.
There are other escape characters you can use to define control characters:
\b
Backspace
\f
Form feed
\n
New line
\r
Carriage return
\t
Tab
Character Arithmetic
You can perform arithmetic on
char
variables. With
myCharacter
containing the character '
X
',
the statement:
myCharacter += 1; // Increment to next character
will result in the value of
myCharacter
being changed to '
Y
'. This is because the Unicode code for
'Y'
is one more than the code for
'X'
. You could use the increment operator
++
to increase the code
stored in
myCharacter
by just writing:
++myCharacter; // Increment to next character
You can use variables of type
char
in an arithmetic expression, and their values will be converted to type
int
to carry out the calculation. It doesn't necessarily make a whole lot of sense, but you could write,
char aChar = 0;
char bChar = '\u0028';
aChar = (char)(2*bChar + 8);
which will leave
aChar
holding the code for
X
- which is 0x0058.
Bitwise Operations
As you already know, all these integer variables we have been talking about are represented internally as
binary numbers. A value of type
int
consists of 32
bi
nary digi
ts
, known to us computer fans as bits. You can
operate on the bit values of integers using the bitwise operators, of which there are four available: