Java Operators

Java Operators is a symbol which is used to perform operations. Java Operators has defined such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.

Java Arithmetic Operators

Java Arithmetic operators are used to perform common mathematical operations.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
SubtractionSubtracts one value from anotherx – y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value from anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x, x++
DecrementDecreases the value of a variable by 1–x, x–

Java Assignment Operators

Java Assignment operators are used to assign values to variables.
Assignment operator (=) to assign the value 5 to a variable called x:

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

Java Relational Operators

Java Relational operators are used to compare two values:

OperatorNameExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Java Logical Operators

Java Logical operators are used to determine the logic between variables or values:

OperatorNameDescriptionExample
&&Logical andReturns true if both statements are truex < 5 && x < 10
||Logical orReturns true if one of the statements is truex < 5 || x < 4
!Logical notReverse the result, returns false if the result is true!(x < 5 && x < 10)

Unary Operators

Unary operators are used to perform on single variablle.

OperatorNameDescriptionExample
+ – !+ve, -ve, notIt is used with one variable+i -i ~ !

Bitwise Operators

Java Bitwise operators are used to perform the operation on bit level.

OperatorNameDescriptionExample
shift<< >> >>>Shift bitsc = a << 2;
bitwise AND&& Operation on two variable on binary levelc = a & b;
bitwise exclusive OR^XOR operation on two variable on binary levelc = a ^ b;
bitwise inclusive OR|OR operation on two variable on binary levelc = a | b;

Ternary Operator



Ternary operators is used as conditional and assignment both operations.

OperatorDescriptionExample
<condition>?<var1>:<var2>ternary(a<b)?x:y
java java tutorials learn java study java