Core Java - Operators


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
  • Unary Operators
  • Ternary Operator
  • InstanceOf Operator

  • 1. The Arithmetic Operators:
    Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
    Assume integer variable A holds 10 and variable B holds 20, then:

        Operator	                    Description                                               Example
        ------    ---------------------------------------------------------------------   ----------------------
        +	        Addition(Adds values on either side of the operator)	                A + B will give 30
    
        -	        Subtraction(Subtracts right hand operand from left hand operand)        A - B will give -10
    
        *	        Multiplication(Multiplies values on either side of the operator)        A * B will give 200
    
        /	        Division(Divides left hand operand by right hand operand)               B / A will give 2
    
        %	        Modulus(Divides left hand operand by right hand operand             	B % A will give 0
                        and returns remainder)
    
    
    Eg:
    The following simple example program demonstrates the arithmetic operators:
        public class Test{
            public static void main(String args[]){
            int a =10;
            int b =20;
            int c =25;
            int d =25;
            System.out.println("a + b : "+(a + b));
            System.out.println("a - b : "+(a - b));
            System.out.println("a * b : "+(a * b));
            System.out.println("b / a : "+(b / a));
            System.out.println("b % a : "+(b % a));
            System.out.println("c % a : "+(c % a));
            System.out.println("a++ : "+(a++));
            System.out.println("b-- : "+(a--));
            //Check the difference in d++ and ++d
            System.out.println("d++ : "+(d++));
            System.out.println("++d : "+(++d));
            }
        }    
    
    
    Output:
    a + b :30
    a - b :-10
    a * b :200
    b / a :2
    b % a :0
    c % a :5
    a++:10
    b-- :11
    d++ :25
    ++d :27

    2. Relational Operators:
    There are following relational operators supported by Java language: Assume variable A holds 10 and variable B holds 20, then:

        Operator        	            Description	                                             Example
        ------    --------------------------------------------------------------        --------------------------
        +	        Addition(Adds values on either side of the operator)	                A + B will give 30
    
        -	        Subtraction(Subtracts right hand operand from left hand operand)        A - B will give -10
    
        *	        Multiplication(Multiplies values on either side of the operator)        A * B will give 200
    
        /	        Division(Divides left hand operand by right hand operand)               B / A will give 2
    
        %	        Modulus(Divides left hand operand by right hand operand and             B % A will give 0
                    returns remainder)	
    
    
    Eg:
    The following simple example program demonstrates the relational operators
        public class Test{
            public static void main(String args[]){
            int a =10;
            int b =20;
            System.out.println("a == b : "+(a == b));
            System.out.println("a != b : "+(a != b));
            System.out.println("a > b : "+(a > b));
            System.out.println("a < b : "+(a < b));
            System.out.println("b >= a : "+(b >= a));
            System.out.println("b <= a : "+(b <= a));
            }
        }  
    
    
    Output:
    a == b :false
    a != b :true
    a > b :false
    a < b :true
    b >= a :true
    b <= a :false

    3. Bitwise Operators:
    Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
    Bitwise operator works on bits and performsbit-by-bit operation.
    Assume if a = 60; and b = 13; now in binary format they will be as follows:

    a = 0011 1100
    b = 0000 1101
    -----------------
    a&b = 0000 1100
    a|b = 0011 1101
    a^b = 0011 0001
    ~a = 1100 0011

    The following table lists the bitwise operators:
    Assume integer variable A holds 60 and variable B holds 13, then:

        Operator               Description                                      Example 
        ------  ----------------------------------------------      -------------------------------------
        &        Binary AND Operator copies a bit to the            (A & B) will give 12 which is 0000 1100
                 result if it exists in both operands.	
    
        |        Binary OR Operator copies a bit if it              (A | B) will give 61 which is 0011 1101
                 exists in either operand.  
    
        ^        Binary XOR Operator copies the bit if it           (A ^ B) will give 49 which is 0011 0001
                 is set in one operand but not both.	
    
        ~        Binary Ones Complement Operator is unary           (~A ) will give -60 which is 1100 0011
                 and has the effect of 'flipping' bits.	
    
        <<       Binary Left Shift Operator. The left operands       A << 2 will give 240 which is 1111 0000
                 value is moved left by the number of bits
                 specified by the right operand.      
    
        >>       Binary Right Shift Operator. The left operands     A >> 2 will give 15 which is 1111
                 value is movedright by the number of bits 
                 specified by the right operand.     
    
        >>>      Shift right zero fill operator.The left operands   A >>>2 will give 15 which is 0000 1111
                 value is movedright by the number of bits  
                 specified by the right operand and shifted 
                 values are filled up with zeros.	         
    
    
    Eg:
    The following simple example program demonstrates the bitwise operators
        public class Test{
            public static void main(String args[]){
            int a =60; /* 60 = 0011 1100 */
            int b =13; /* 13 = 0000 1101 */
            int c =0;
             c = a & b;/* 12 = 0000 1100 */
            System.out.println("a & b : "+ c );
             c = a | b;/* 61 = 0011 1101 */
            System.out.println("a | b : "+ c );
             c = a ^ b;/* 49 = 0011 0001 */
            System.out.println("a ^ b : "+ c );
             c =~a;/*-61 = 1100 0011 */
            System.out.println("~a : "+ c );
             c = a << 2;/* 240 = 1111 0000 */
            System.out.println("a << 2 : "+ c );
             c = a >>2;/* 215 = 1111 */
            System.out.println("a >> 2 : "+ c );
             c = a >>>2;/* 215 = 0000 1111 */
            System.out.println("a >>> 2 : "+ c );
            }
        }
    
    
    Output:
    a & b :12 
    a | b :61 
    a ^ b :49 
    a : -61  
    a << 2 : 240    
    a >> 2 : 15   
    a >>> 2 : 15 
    
    
    4. Logical Operators:
    The following table lists the logical operators: Assume Boolean variables A holds true and variable B holds false, then:

      Operator	                    Description	                                         Example
       ------  -------------------------------------------------------------------  ------------------------
        &&	    Called Logical AND operator.                                            (A && B) is false.
                If both the operands are non-zero, then the condition becomes true.
    
        ||	    Called Logical OR Operator. If any of the two operands are non-zero,    (A || B) is true.
                then the condition becomes true.	
    
        !	    Called Logical NOT Operator.                                            !(A && B) is true.
                Use to reverses the logical state of its operand. 
                If a condition is true then Logical NOT operator will make false.
    
    
    Eg:
    The following simple example program demonstrates the logical operators.
        public class Test{
            public static void main(String args[]){
            boolean a =true;
            boolean b =false;
            System.out.println("a && b : "+(a&&b));
            System.out.println("a || b : "+(a||b));
            System.out.println("!(a && b) : "+!(a && b));
            }
        }
    
    
    Output:
    a && b : false
    a || b : true
    !(a && b): true

    5. Assignment Operators:
    There are following assignment operators supported by Java language:

        Operator	            Description	                                                    Example
        ------  -------------------------------------------------------------------  ----------------------------------
        +=	    Add AND assignment operator, It adds right operand to the left        C += A is equivalent to C = C + A
                operand and assign the result to left operand.	
    
        -=	    Subtract AND assignment operator, It subtracts right operand          C -= A is equivalent to C = C - A 
                from the left operand and assign the result to left operand.
    
        *=	    Multiply AND assignment operator, It multiplies right operand         C *= A is equivalent to C = C * A 
                with the left operand and assign the result to left operand.
    
        /=	    Divide AND assignment operator, It divides left operand with          C /= A is equivalent to C = C / A
                the right operand and assign the result to left operand.	
                
        %=	    Modulus AND assignment operator, It takes modulus using two           C %= A is equivalent to C = C % A
                operands and assign the result to left operand.	
    
    
    Eg:
    The following simple example program demonstrates the assignment operators
        public class Test{
            public static void main(String args[]){
            int a =10;
            int b =20;
            int c =0;
             c = a + b;
            System.out.println("c = a + b : "+ c );
             c += a ;
            System.out.println("c += a : "+ c );
             c -= a ;
            System.out.println("c -= a : "+ c );
             c *= a ;
            System.out.println("c *= a : "+ c );
             a =10;
             c =15;
             c /= a ;
            System.out.println("c /= a : "+ c );
            a =10;
            c =15;
            c %= a ;
            System.out.println("c %= a = "+ c );
            c <<=2;
            System.out.println("c <<= 2 = "+ c );
            c >>=2;
            System.out.println("c >>= 2 = "+ c );
            c >>=2;
            System.out.println("c >>= a = "+ c );
            c &= a ;
            System.out.println("c &= 2 = "+ c );
            c ^= a ;
            System.out.println("c ^= a = "+ c );
            c |= a ;
            System.out.println("c |= a = "+ c );
            }
        }
    
    
    Output:
    c = a + b =30
    c += a =40
    c -= a =30
    c *= a =300
    c /= a =1
    c %= a =5
    c <<=2=20
    c >>=2=5
    c >>=2=1
    c &= a =0
    c ^= a =10
    c |= a =10

    6. Unary Operators
    There are following assignment operators supported by Java language:

        Operator	            Description	                                                            Example
        ------  -------------------------------------------------------------------             --------------------------
        –	    Unary minus, used for negating the values.	                                        (A - B) is C.
    
        +	    Unary plus indicates the positive value (numbers are positive without this).        (A + B) is C.
                However It performs an automatic conversion to int when the type of its 
                operand is the byte, char, or short. This is called unary numeric promotion.
    
        ++	    Increment operator, used for incrementing the value by 
                There are two varieties of increment operators.
                Post-Increment: Value is first used for computing the result and then incremented.  (A++ + B) is C.
                Pre-Increment: Value is incremented first, and then the result is computed	        (++A + B) is C.
    
        - -	    Decrement operator, used for decrementing the value by 
                There are two varieties of decrement operators.
                Post-decrement: Value is first used for computing the result and then decremented.  (A-- - B) is D.
                Pre-Decrement: Value is decremented first, and then the result is computed.	        (--A - B) is D.
    
        !	    Logical not operator, used for inverting a boolean value.                           !(A < B && C < D) is E.
    
    
    7. Ternary Operator(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.
    Syntax:
        variable x =(expression)? value (iftrue): value (iffalse)
    
    Eg:
    The following simple example program demonstrates the ternary operators
        public class Test{
            public static void main(String args[]){
            int a , b;
            a =10;
            b =(a ==1)?20:30;
           System.out.println("Value of b is : "+ b );
            b =(a ==10)?20:30;
           System.out.println("Value of b is : "+ b );
           }
        }
    
    
    Output:
    Value of b is:30
    Value of b is:20

    8. instanceof Operator:
    This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type).
    Syntax:
        (Object reference variable ) instanceof (class/interface type)
    
    
    Eg:
    The following simple example program demonstrates the instanceof operators
        class Vehicle{}
        public class CarextendsVehicle{
            public static void main(String args[]){
            Vehicle a =newCar();
            boolean result = a instanceofCar;
            System.out.println(result);
            }
        }
    
    
    Output:
    true

    (Core Java - Control Statements)