Java Reference
In-Depth Information
character by character. Each such character is of type
char
. A character literal is
enclosed in single quotation marks and includes just one character:
'a' 'm' 'X' '!' '3' '\\'
All of these examples are of type
char
. Notice that the last example uses an
escape sequence to represent the backslash character. You can even refer to the single
quotation character using an escape sequence:
'\''
Finally, the primitive type
boolean
stores logical information. We won't be
exploring the use of type
boolean
until we reach Chapter 4 and see how to introduce
logical tests into our programs, but for completeness, we include the
boolean
literal
values here. Logic deals with just two possibilities: true and false. These two Java
keywords are the two literal values of type
boolean
:
true false
The basic arithmetic operators are shown in Table 2.2. The addition and subtraction
operators will, of course, look familiar to you, as should the asterisk as a multiplication
operator and the forward slash as a division operator. However, as you'll see, Java has
two different division operations. The remainder or mod operation may be unfamiliar.
Division presents a problem when the operands are integers. When you divide 119
by 5, for example, you do not get an integer result. Therefore, the results of integer
division are expressed as two different integers, a quotient and a remainder:
119
5
23
1
quotient
2
with 4
1
remainder
2
In terms of the arithmetic operators:
119 / 5
evaluates to
23
119 % 5
evaluates to
4
Table 2.2
Arithmetic Operators in Java
Operator
Meaning
Example
Result
addition
+
2 + 2
4
subtraction
-
53 - 18
35
multiplication
*
3 * 8
24
division
/
4.8 / 2.0
2.4
remainder or mod
%
19 % 5
4
Search WWH ::
Custom Search