Operators in Java

Operators are symbols which is used to do some operations. For example if our operation is addition, we use + symbol and that is our operator. In any operation we have operator and operand. Look at this example:
A+B : Here, A and B are operator and + is operand. Whole expression is called operation.

Types of Operators in Java

  • Unary Operators
  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Shift Operators
  • Logical Operators
  • Ternary Operator
  • Assignment Operators

Unary Operators

Those operators which needs only one operand are called unary operators. Click here to know how unary operator works.

OperatorNameDescriptionExample
++IncrementIncreases value by 1a++, ++a
--DecrementDecreases value by 1a--, --a

Arithmetic Operators

Those operators which are used to do basic math operations or arithmetic operations are called arithmetic operators.

OperatorNameDescriptionExample
+AdditionAdds two valuesa + b
-SubtractionSubtracts one value from anothera - b
*MultiplicationMultiplies two valuesa * b
/DivisionDivides one value by anothera / b
%ModulusReturns the division remaindera % b

Relational Operators

Those operators which are used to compare two values are called relational operators.

OperatorNameExample
>Greater thana > b
<Less thana < b
>=Greather than or equal toa >= b
<=Less than or equal toa <= b
==Equal toa == b
!=Not equal toa != b

Bitwise Operators

These operators converts each value in binary and does logical operation in bits. Click here to know how bitwise operator works.

OperatorNameExample
&ANDa & b
|ORa | b
~NOTa ~ b
^XORa ^ b

Shift Operators

These operators converts each value in binary and shift bits left or right. Click here to know how shift operator works.

OperatorNameExampleSame as
<<Left shifta << ba * 2 ^ b
>>Right shifta >> ba / 2 ^ b

Logical Operators

Those operators which are used to determine the logic between variables or values are called logical operators.

OperatorNameDescriptionExample
&&ANDTrue if both values are truea && b
||ORFalse if both values are falsea || b
!NOTComplements the valuea ! b

Ternary Operator

This operator needs three operands and executes with some condition. It is same as if statement which we will learn later.

OperatorNameDescriptionExample
? :Ternary IfCondition ? True : Falsea > b ? a - b : b - a

Assignment Operator

This operator is used to assign some value to the variable. We have been using one since beginning. Remember =, yes it is also assignment operator. Take a loot at more:

OperatorExampleSame as
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5

Take a look at this example:

This example shows how to use operators in java:

public class Operators { public static void main(String[] args) { int a = 5; int b = 4; System.out.println(a+b); System.out.println(a%b); System.out.println(a>b); System.out.println(b>a ? b-a : a-b); System.out.println(++a); System.out.println(--b); } }
Also we can assign operation to new variable and print new variable. Example: int c = a + b; and print c. They both are same.

Output:

9
1
true
1
6
3


Comments in Java Previous Next Associativity and Precedence of Operators in Java