Java Reference
In-Depth Information
LITERALS (CONSTANTS)
Some authors call values such as 23 and -67 integer literals or integer constants or
simply integers; values such as 12.34 and 25.60 are called floating-point literals
or floating-point constants or simply floating-point numbers; and values such as
'a' and '5' are called character literals, character constants, or simply characters.
Arithmetic Operators and Operator Precedence
One of the most important features of a computer is its ability to calculate. You can
use the standard arithmetic operators to manipulate integral and floating-point data types.
Java has five arithmetic operators:
Arithmetic Operators: + (addition), - (subtraction or negation), * (multiplication),
/ (division), % (mod,(modulus or remainder))
You can use these operators with both integral and floating-point data types. When you use
/ with the integral data type, it gives the quotient in integer form. That is, integral division
truncates any fractional part; there is no rounding. Similarly, when you use % with the
integral data type, it gives the remainder in integer form. (Examples 2-2 and 2-3 clarify how
the operators / and % work with integral and floating-point data types.)
Since junior high school, you have probably worked with arithmetic expressions such as
the following:
(i) -5
(ii) 8 - 7
(iii) 3 + 4
(iv) 2 + 3 * 5
(v) 5.6 + 6.2 * 3
(vi) x + 2 * 5 + 6 / y
In expression (vi), x and y are some unknown numbers. Formally, an arithmetic
expression is constructed by using arithmetic operators and numbers. The numbers and
alphabetical symbols in the expression are called operands. Moreover, the numbers and
alphabetical symbols used to evaluate an operator are called the operands for that operator.
In expression (i), the operator - (subtraction) is used to specify that the number 5 is
negative. Moreover, in the expression -5 , - has only one operand, which is 5 . Operators
that have only one operand are called unary operators.
In expression (ii), the symbol - is used to subtract 7 from 8 . In this expression, - has two
operands, 8 and 7 . Operators that have two operands are called binary operators.
Unary operator: An operator that has only one operand.
Binary operator: An operator that has two operands.
 
Search WWH ::




Custom Search