
Home
|


JAVA PROGRAMMING
Introduction to
Operators
An operator takes one or more arguments and produces
a new value. The arguments are in a different form than ordinary method
calls, but the effect is the same. You should be reasonably comfortable
with the general concept of operators from your previous programming
experience. Addition (+), subtraction and unary minus (-), multiplication
(*), division (/) and assignment (=) all work much the same in any
programming language.
Mathematical operators
The basic mathematical operators are the same as the
ones available in most programming languages: addition (+), subtraction
(-), division (/), multiplication (*) and modulus (%, produces the
remainder from integer division). Integer division truncates, rather than
rounds, the result.
Java also uses a shorthand notation to perform an operation and an
assignment at the same time. This is denoted by an operator followed by an
equal sign, and is consistent with all the operators in the language
(whenever it makes sense). For example, to add 4 to the variable x and
assign the result to x, use: x += 4;.
This example shows the use of the mathematical
operators:
Auto increment and decrement
Java, like C, is full of shortcuts. Shortcuts can make code
much easier to type, and either easier or harder to read. Two of the nicer
shortcuts are the increment and decrement operators (often referred to as
the auto-increment and auto-decrement operators). The decrement operator
is -- and means "decrease by one unit." The increment operator
is ++ and means "increase by one unit." If A is an int, for
example, the expression ++A is equivalent to (A = A + 1). Increment and
decrement operators produce the value of the variable as a result.
There are two versions of each type of operator, often called the prefix
and postfix versions. Pre-increment means the ++ operator appears before
the variable or expression, and post-increment means the ++ operator
appears after the variable or expression. Similarly, pre-decrement means
the -- operator appears before the variable or expression, and
post-decrement means the -- operator appears after the variable or
expression. For pre-increment and pre-decrement, (i.e., ++A or --A), the
operation is performed and the value is produced. For post-increment and
post-decrement (i.e. A++ or A--), the value is produced, then the
operation is performed.
Relational operators
Relational operators generate a boolean result. They evaluate
the relationship between the values of the operands. A relational
expression produces true if the relationship is true, and false if the
relationship is untrue. The relational operators are less than (<),
greater than (>), less than or equal to (<=), greater than or equal
to (>=), equivalent (==) and not equivalent (!=). Equivalence and
nonequivalence works with all built-in data types, but the other
comparisons won't work with type boolean.
Testing object equivalence
The relational operators == and != also work with all objects, but their
meaning often confuses the first-time Java programmer.
Logical operators
The logical operators AND (&&), OR (||) and NOT (!) produce a
boolean value of true or false based on the logical relationship of its
arguments. This example uses the relational and logical operators:
You can apply AND, OR, or NOT to boolean values only. You can't use a non-boolean
as if it were a boolean in a logical expression as you can in C and C++.
You can see the failed attempts at doing this commented out with a //!
comment marker. The subsequent expressions, however, produce boolean
values using relational comparisons, then use logical operations on the
results.
One output listing looked like this:
i = 85
j = 4
i > j is true
i < j is false
i >= j is true
i <= j is false
i == j is false
i != j is true
(i < 10) && (j < 10) is false
(i < 10) || (j < 10) is true
Note that a boolean value is automatically converted to an appropriate
text form if it's used where a String is expected.
A sample code using Arithmetic operators


 |