Operators
Operator are basically two types:
- Arithmetic Operators
- Logical Operators
The Arithmetic Operators
The Java
programming language provides operators that perform addition, subtraction,
multiplication, and division. There's a good chance you'll recognize them by
their counterparts in basic mathematics. The only symbol that might look new to
you is "%", which divides one operand by another and returns the
remainder as its result.
Operator
|
Description
|
+
|
Additive
operator (also used for String concatenation)
|
-
|
Subtraction
operator
|
*
|
Multiplication
operator
|
/
|
Division
operator
|
%
|
Remainder
operator
|
The
Unary Operators
The unary operators require only one
operand; they perform various operations such as incrementing/decrementing a
value by one, negating an expression, or inverting the value of a Boolean.
Operator
|
Description
|
+
|
Unary plus operator; indicates
positive value (numbers are positive without this, however)
|
-
|
Unary minus operator; negates an
expression
|
++
|
Increment operator; increments a
value by 1
|
--
|
Decrement operator; decrements a
value by 1
|
!
|
Logical complement operator;
inverts the value of a boolean
|
Logical operators are mainly used to control program flow. Usually, you will find
them as part of an if, a while, or some other control statement
The Logical operators are:
op1 && op2
|
Performs a logical AND of the two operands.
|
op1 || op2
|
Performs a logical OR of the two operands.
|
!op1
|
Performs a logical NOT of the operand.
|
The Equality and Relational
Operators
The
equality and relational operators determine if one operand is greater than,
less than, equal to, or not equal to another operand. The majority of these
operators will probably look familiar to you as well. Keep in mind that you
must use "==", not "=", when testing if two primitive
values are equal.
==
|
equal
to
|
!=
|
not
equal to
|
>
|
greater
than
|
>=
|
greater
than or equal to
|
<
|
less
than
|
<=
|
less
than or equal to
|