Lecture - 5 Types of Operators in Java


What are Operators ?

Operators are used to perform operations on variables and values for eg:
            
            7 + 111 = 118 (Result) , where 7 and 111 are operands and '+' is an operator

Types Of Operators:

(1) Arithmetic Operators : The Operation perform between 2 integer such as addition, subtraction ,                                                       division and multiplication etc. 
        
for eg:
            2+2=4 (Addition)
            3-2=1 (Subtraction)
            4X4=16 (Multiplication)
            16/4=4  (Division)
            12%3=0 (Modulo Operator)
            12++ = 12+1  (Increment Operator)
            12-- = 12-1  (Decrement Operator)

Additional Operator gives you sum of two values (Operands) as the output in return
Subtraction Operator gives you subtraction of two values (Operands) as the output in return
Multiplication Operator gives you product of two values (Operands) as the output in return
Division Operator gives you quotient of two values (Operands) as the output in return
Modulo Operator gives you remainder of two values (Operands) as the output in return
Increment Operator gives you "+1" value in return
Decrement Operator gives you "-1" value in return 

(2) Assignment Operators : To Assign The value from literal to a variable.

for eg:     
            int number = 2;  ("=" is the assignment operator here)  
            number += 10;   ("+=" is the addition assignment operator which means "number=number+10")
            number -= 10;   ("-=" is the subtraction assignment operator which means "number=number-10")
            number *= 10;   ("*=" is the multiplication assignment operator which means                                                                            "number=number*10")
            number /= 10;   ("/=" is the division assignment operator which means "number=number/10")
            number %= 10;   ("%=" is the modulo assignment operator which means "number=number%10")

(3) Comparison Operator : This operator is used to compare two value no matter of what data type in one go.

for eg: 
            System.out.println(2==2);  (The answer will be True because the "2" is equal to another "2")
            System.out.println(2==6);  (The answer will be False because the "2" is not equal to another "2")
            System.out.println(6>=2);  (The answer will be True because the "6" is greater than "2")
            System.out.println(78<=299);  (The answer will be True because the "299" is greater than "78")
            

(4)  Logical Operators in Java : Logical Operators may decide what to do and what not to do on the basis of logic that they work on.

for eg: There are mainly 2 types of logical operators ie "AND"  and "OR" 

        AND operator gives the output "True" if any one statement is True
        AND operator gives the output "False" if any one statement is false
        OR operator gives the output "True" if any one statement is true

        System.out.println((4>2) && (4<6));  TRUE
        System.out.println((4>2) && (4>6));  FALSE
        System.out.println((4>2) || (4<6));  TRUE
        System.out.println((4<2) || (4<6));  TRUE

Please Download Notes PDF from here : Click Here

                                                                                                                                    Next Page>>

CODE SNIPPET IS SHOWN BELOW

package typesofoperators;
/*

types of operators in java

*/
public class Typesofoperators {

    public static void main(String[] args) {
            
        /*
        Arithmetic Operators in java
        
        first =  '+'
        Second = '-'
        Third =  '*'
        Fourth = '/'
        Fifth =  '%'
        Sixth =   Increment Operator = '++'
        Seventh = Decrement Operator = '--'
        
        */
        
        // Additional Operator
        int varA = 34+45;
        System.out.println(varA);
        
        // Subtractional Operator
        int varS = 75-45;
        System.out.println(varS);
        
        // Multiplication Operator
        int varM = 34*4;
        System.out.println(varM);
        
        // Division Operator
        int varD = 75/5;
        System.out.println(varD);
        
        // modulo operator in java
        
        int a = 12%6;
        int b = 22%5;
        System.out.println(b);
        
        
        // increment operator in java
        
        // pre increment operator in java
        int c = 22;
        ++c;
        System.out.println(c);
        
        // post increment operator in java
        int d = 22;
        d++;
        System.out.println(d);
        
        // pre decrement operator in java
        int cd = 22;
        --cd;
        System.out.println(cd);
        
        // post increment operator in java
        int dd = 22;
        dd--;
        System.out.println(dd);
        
        /*
        
        Assignment Operators in Java
        
        The operator which assigns value to a variable is called Assignment Operator
        
        Assignment operator => "="
        AdditionEqualto => "+="
        SubtractionEqualto => "-="
        DivisonEqualto => "/="
        MultiplicationEqualto => "*="
        ModuloEqualto => "%="
        
        */
        
        int AE ;
        AE = 12;
        System.out.println(AE);
        AE += 13;
        System.out.println(AE);
        AE -=3;
        System.out.println(AE);
        AE *= 4;
        System.out.println(AE);
        AE %= 50;
        System.out.println(AE);
        
        
        /*
        
        Comparison Operator in java
        
        Comparison Operator => "=="
        GreaterThanEqualTo => ">="
        LessThanEqualTo => "<="
        
        always return awnser in true or false
        */
        
        System.out.println(4==4); // true
        System.out.println(4==3); // false
        System.out.println(4>=4); // true
        System.out.println(4<=4); // true
        
        
        /*
        
        Logical Operators in Java
        
        AND operator/operation = "&&"
        OR operator/operation  = "||"
        
        */
        
        System.out.println(4==4 && 89==6); // = false
        System.out.println(4==4 || 89==6); // = true
        
        
        /*
        
        Bitwise Operator 
        
        Bin    value
        00  =  0
        01  =  1
        
        10  =  2
        11  =  3
        -----
        10  =  2
        11  =  3
        
        */
        
        System.out.println(2&3);
        System.out.println(2|3);
    }
    
}