Java Reference
In-Depth Information
Table 3-8
Comparison Operators in Java
OPERATOR
OPERATION
SYMBOL
TRUE EXPRESSION
FALSE EXPRESSION
less than
<
(2 < 9)
(9 < 2)
greater than
>
(5 > 1)
(1 > 5)
less than or equal to
<=
(3 <= 4)
(5 <= 4)
greater than or equal to
>=
(8 >= 6)
(3 >= 7)
equal to
==
(9 == 9)
(5 == 9)
not equal to
!=
(4 != 2)
(2 != 2)
The first four comparison operators sometimes are referred to as relational
operators because they compare the relation of two values; the last two some-
times are called equality operators . The double equal sign, ==, is used to differ-
entiate the equal to operation from the assignment operator (=) used in Java.
Spaces are not included between comparison operator symbols.
Expressions
When using operators in code, it is important to understand their functions,
order of precedence, and purpose in the statement expression. In Java and other
programming languages, an expression can perform a calculation, manipulate
characters, call a method, or test data. Expressions can be divided into two basic
categories: numeric and conditional.
Numeric Expressions
A numeric expression is any expression that can be evaluated as a number.
A numeric expression can include arithmetic operators, values, and variables, as
well as methods. The data type of any value in an arithmetic expression must be
one of the numeric primitive data types from Table 3-2 on page 140. A numeric
expression cannot contain String variables, String literals, or objects.
The values, variables, and methods in a numeric expression often are sepa-
rated from each other by parentheses and arithmetic operators. A programmer
must be concerned with both the form and the evaluation of an expression. It is
necessary to consider the purpose of the expression as well as the rules for form-
ing a valid expression before you start to write expressions in Java statements
with confidence.
Forming Valid Numeric Expressions
The definition of a numeric expression dictates the manner in which a
numeric expression can be validly formed. For example, the following statement
formed to assign the identifier, response, twice the value of myAnswer is invalid:
response = 2myAnswer; //Invalid statement
 
Search WWH ::




Custom Search