Assignment Operator:
There are following assignment operators supported by Java language:
Operator Description Example
Simple assignment operator, Assigns values from right side C = A + B will
=
operands to left side operand assign value of A
+ B into C
Add AND assignment operator, It adds right operand to the left C += A is
+=
operand and assign the result to left operand equivalent to C =
C+A
Subtract AND assignment operator, It subtracts right operand C -= A is
-=
from the left operand and assign the result to left operand equivalent to C =
C-A
Multiply AND assignment operator, It multiplies right operand C *= A is
*=
with the left operand and assign the result to left operand equivalent to C =
C*A
Divide AND assignment operator, It divides left operand with the right C /= A is
/=
operand and assign the result to left operand equivalent to C
=C/A
Modulus AND assignment operator, It takes modulus using two C %= A is
%=
operands and assign the result to left operand equivalent to C =
C%A
C <<= 2 is same
<<= Left shift AND assignment operator
as C = C << 2
C >>= 2 is same
>>= Right shift AND assignment operator
as C = C >> 2
C &= 2 is same as
&= Bitwise AND assignment operator
C
=C&2
C ^= 2 is same as
^= bitwise exclusive OR and assignment operator
C
=C^2
C |= 2 is same as
|= bitwise inclusive OR and assignment operator
C
=C|2
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide
which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
EXAMPLE
public class Test {
public static void main(String
args[]){ int a , b;
a = 10;
b = (a == 1) ? 20: 30;
[Link]( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
[Link]( "Value of b is : " + b );
}
}
This would produce the following result:
Value of b is
: 30 Value of
b is : 20
Operator Precedence:
PR Operator precedence determines the grouping of terms in an expression. This affects
how an expression is evaluated. Certain operators have higher precedence than others; for
example, the multiplication operator has higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher
precedence than
+, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Category Operator Associativity
Postfix () [] . (dot operator) Left to right
Unary ++ - - ! ~ Right to left
Multiplicati */% Left to right
ve
Additive +- Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise & Left to right
AND
Bitwise ^ Left to right
XOR
Bitwise OR | Left to right
Logical && Left to right
AND
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>> = <<= &= ^= |= Right to left
Comma , Left to right
ECEDENCE OF JAVA OPERATORS