Operators
Java provides a rich operator environment. Most of its operators can be divided
into the following four groups: arithmetic, bitwise, relational, and logical. Java also
defines some additional operators that handle certain special situations.
Operators are used to perfom operations on operends and in turn return result based on those operations.
Postfix operators |
[] . (parameters) expression++ expression-- |
Unary prefix operators |
++expression , --expression , +expression , -expression , ~, ! |
Unary prefix creation and cast |
new (type) |
Multiplicative |
* / % |
Additive |
+ - |
Shift |
<< >> >>> |
Relational |
< <= > >= instanceof |
Equality |
== != |
Bitwise/logical AND |
& |
Bitwise/logical XOR |
^ |
Bitwise/logical OR |
| |
Conditional AND |
&& |
Conditional OR |
|| |
Conditional |
?: |
Assignment |
= += -= *= /= %= <<= >>= >>>= &= ^= |= |
Figure showing the list of operaters used in java
Simple Assignment Operator =
The assignment statement has the following syntax:-
<variable> = <expression>
which can be read as "the destination <variable>, gets the value of the source <expression>". The previous value of the destination variable is overwritten by the assignment operator = . The destination <variable> and the source <expression> must be type compatible. The destination variable must also have been
declared. Since variables can store either primitive data values or object references, <expression> evaluates to either a primitive data
value or an object reference.
Assigning Primitive Values
The following examples illustrate assignment of primitive values:
int j, k;
j = 10; // j gets the value 10.
j = 5; // j gets the value 5. Previous value is overwritten.
Arithmetic Operators:
* , / , % , + , -
Unary Arithmatic Operators
The unary operators have the highest precedence of all the arithmetic operators. The unary operator - negates the numeric value of its
operand. The following example illustrates the function of unary operators:
int value = - -10; // (-(-10)) is 10
Notice the blank needed to separate the unary operators; otherwise, these would be interpreted as the decrement operator -- . The unary operator + has no effect on the evaluation of the operand value.
Multiplicative Binary Operators: *, /, %
Multiplication Operator: * .
Multiplication operator * multiplies two numbers.
int sameSigns = -4 * -8; // result: 32
double oppositeSigns = 4.0 * -8.0; // result: -32.0
int zero = 0 * -0; // result: 0
Division Operator / -:
Integer division always returns the quotient as an integer value, i.e. the result is truncated toward zero. Note that the division performed is integer division if the operands have integral values, even if the result will be stored in a floating-point type.An ArithmeticException is thrown when attempting integer division with zero, meaning that integer division by zero is an illegal operation.
If any of the operands is a floating-point type, the operation performs floating-point division.
Examples:-
int var1 = 4 / 5; // result: 0
int var2= 8 / 8; // result: 1
double dvar = 12 / 8; // result: 1 by integer division. d1 gets the value 1.0.
Remainder Operator: %
In mathematics, when we divide a number (the dividend) by a another number (the divisor), the result can be expressed in terms of a
quotient and a remainder. For example, dividing 7 by 5, the quotient is 1 and the remainder is 2. The remainder operator % returns the
remainder of the division performed on the operands.
Example:-
int quotient = 7 / 5; // Integer division operation: 1
int remainder = 7 % 5; // Integer remainder operation: 2
Additive Binary Operators: +, -
The addition operator + and the subtraction operator - behave as their names imply: add or subtract values. The binary operator + also
acts as string concatenation if any of the operands is a string .
Additive operators have lower precedence than all the other arithmetic operators.
int var1=10, var2 =10;
int var3=var1+ var2 //result will be 20
Boolean Logical Operators: !, ^, &, |
Boolean logical operators include the unary operator ! (logical complement) and the binary operators & (logical AND), | (logical inclusive
OR), and ^ (logical exclusive OR, a.k.a. logical XOR). Boolean logical operators can be applied to boolean operands, returning a boolean
value. The operators &, |, and ^ can also be applied to integral operands to perform bitwise logical operations .
Given that x and y represent boolean expressions.
Logical complement !x Returns the complement of the truth-value of x.
Logical AND x & y Returnstrue if both operands are true; otherwise, false.
Logical OR x | y true if either or both operands are true; otherwise,false.
Logical XOR x ^ y true if and only if one operand is true; otherwise, false.
These operators always evaluate both the operands, unlike their counterpart conditional operators && and || .
Examples:-
boolean b1, b2, b3 = false, b4 = false;
b1 = 4 == 2 & 1 < 4; // false, evaluated as (b1 = ((4 == 2) & (1 < 4)))
b2 = b1 | !(2.5 >= 8); // true
b3 = b3 ^ b2; // true
b4 = b4 | b1 & b2; // false
Order of evaluation is illustrated for the last example:-
(b4 = (b4 | (b1 & b2)))
(b4 = (false | (b1 & b2)))
(b4 = (false | (false & b2)))
(b4 = (false | (false & true)))
(b4 = (false | false))
(b4 = false)
Relational Operators: <, <=, >, >=
a < b |
a less than b? |
a <= b |
a less than or equal to b? |
a > b |
a greater than b? |
a >= b |
a greater than or equal to b? |
All relational operators are binary operators, and their operands are numeric expressions.The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic
operators, but higher than that of the assignment operators.
Examples:-
double hours = 45.5;
boolean overtime = hours >= 35.0; // true.
boolean order = 'A' < 'a'; // true. Binary numeric promotion applied.
Conditional Operators: &&, ||
Conditional operators && and || are similar to their counterpart logical operators & and | , except that their evaluation is short-circuited.
Given that x and y represent values of boolean expressions, the conditional operators are given below.
Conditional AND :- x && y true if both operands are true; otherwise, false.
Conditional OR:- x || y true if either or both operands are true; else false.
Examples:-
boolean b1 = 4 == 2 && 1 < 4; // false, short-circuit evaluated as
//(b1 = ((4 == 2) && (1 < 4)))
boolean b2 = !b1 || 2.5 > 8; // true, short-circuit evaluated as
// (b2 = ((!b1) || (2.5 > 8)))
boolean b3 = !(b1 && b2); // true
boolean b4 = b1 || !b3 && b2; // false, short-circuit evaluated as
// (b4 = (b1 || ((!b3) && b2)))
|