Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −

    Arithmetic Operators

    Relational Operators

    Bitwise Operators

    Logical Operators

    Assignment Operators

    Miscellaneous Operators


Arithmetic Operator

It is used to do mathematic calculation like: + , - , * , / , % etc.

Example if a=4 and b=2

    Addition (+) 

    Adds values on either side of the operator.

          a + b = 6


    Subtraction (-)  

    Subtract right-hand operand from left-hand operand.

         a - b = 2


    Multiplication (*)  

    Multiplies values on either side of the operator.

         a * b = 8


    Division (/)  

    Divides left-hand operand by right-hand operand.  

         a / b = 2


    Modulus (%) 

    Divides left-hand operand by right-hand operand and returns remainder.

         a % b = 0


    Increment (++) Decrement (--)

    Increment (++) adds one to each value.  Decrement (--) subtract one to each value

    It is of two types

        1. Prefix ++ , --

        2. Postfix ++ , --

        Prefix -> It first add the value and then operates

        Postfix -> It first operates and then add value

             a++ = 5

             a-- = 3


Relational Operators

It is also known as comparison operator it determine the relation between the value its answer come in true or false. (< > , <= , >= , == , !=)

Example if a=10 and b=7

    Greater than (>)     

    Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.           

        a > b = false


    Less then (<)  

    Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

         a < b = true


    Greater then equal to (>=)  

    Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.   

         a >= b = false


    Less then equal to (<=)  

    Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

         a <= b = true


    Equal to (==)

    Checks if the values of two operands are equal or not, if yes then condition becomes true. 

            a == b = false


    Not equal to (!=)  

    Checks if the values of two operands are equal or not, if yes then condition becomes true.

             a != b = true  


Bitwise Operator

Bitwise operator works on bits and perform bit-by-bit operation. Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.                 (& , | , ^ , >> , << , ~,>>>)

Example if a = 60 and b = 13

      a = 0011 1100

     b = 0000 1101

Bitwise AND [&] 

    Binary AND Operator copies a bit to the result. if it exists in both operands.

    Example - (A & B) will give 12 which is 0000 1100


Bitwise OR [|] 

    Binary OR Operator copies a bit if it exists in either operand.

    Example - (A | B) will give 61 which is 0011 1101


Bitwise XOR [^]

    Binary XOR Operator copies the bit if it is set in one operand but not both.

    Example - (A ^ B) will give 49 which is 0011 0001


Bitwise Right Sift [>>] 

    Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

    Example - A >> 2 will give 15 which is 0000 1111


Bitwise Lift Sift [<<]

    Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

    Example - A << 2 will give 240 which is 1111 0000


Ones Complement [~]

     Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.

    Example - (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.


Zero fill right shift[>>>]

    Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

    Example -  A >>>2 will give 15 which is 0000 1111


Logical Operators

It is used to combine the existing expression .It is also used to examine the arithmetic and relational operator. It is of 3 type

Logical AND [&&]  

    When both the condition are true then it works

    Experation 1 && Experation 2

    False -> no

    Post increment/decrement in exp1 and store 0(zero) left side variable.

     Experation 1 && Experation 2

         True -> yes

    Post increment/decrement in exp1 and check exp2

    If exp2 false

    Post increment/decrement in exp2

    Store 0(zero) into left side variable

    If exp2 true

         Post increment/decrement in exp2

         Store 1(one) into left side variable

    Example - (A && B) is false

 

Logical OR [||]

    When any one condition is true then it operates

    Example - (A || B) is true


Logical NOT [!]

    It reverses the value of the operands

    Example - !(A && B) is true


Assignment Operators

This operator is used to store a value in variable of any variable. In left part is always used to variable.

Simple assignment operator (=)

    Assigns values from right side operands to left side operand. 

    Example - C = A + B will assign value of A + B into C


 Add AND assignment operator (+=)

    It adds right operand to the left operand and assign the result to left operand. 

    Example - C += A is equivalent to C = C + A


Subtract AND assignment operator (-=)

     It subtracts right operand from the left operand and assign the result to left operand.

     Example - C -= A is equivalent to C = C - A


Multiply AND assignment operator (*=)

     It multiplies right operand with the left operand and assign the result to left operand. 

    Example - C *= A is equivalent to C = C * A


Divide AND assignment operator (/=)

    It divides left operand with the right operand and assign the result to left operand.

    Example - C /= A is equivalent to C = C / A


Modulus AND assignment operator (%=)

    It takes modulus using two operands and assign the result to left operand.

    Example - C %= A is equivalent to C = C % A


 Left shift AND assignment operator (<<=)

    Example - C <<= 2 is same as C = C << 2


Right shift AND assignment operator (>>=)

    Example - C >>= 2 is same as C = C >> 2


Bitwise AND assignment operator (&=)

    Example - C &= 2 is same as C = C & 2


Bitwise exclusive OR and assignment operator (^=)

    Example - C ^= 2 is same as C = C ^ 2


Bitwise inclusive OR and assignment operator (|=)

    Example - C |= 2 is same as C = C | 2


Miscellaneous/Conditional/Ternary Operator

It stores value depends upon a condition It is also known as ternary operator syntax:-

Condition? True: False