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
  • 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. 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

    4. 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

    5. 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.
    
    
    6. 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

    7. 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

    ☛ Join to Learn from Experts: Java / J2EE Training by TesDBAcademy
    (Core Java - Control Statements)