dsa

Java Operators

In this tutorial, you'll learn about different types of operators in Java, their syntax and how to use them with the help of examples.

Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are-

1. Arithmetic Operators

Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.
1. * : Multiplication
2. / : Division
3. % : Modulo
4. + : Addition
5. - : Subtraction

EXAMPLE:


// Java program to illustrate 
// arithmetic operators 
public class operators { 
    public static void main(String[] arg)
    { 
        int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30; 
        String x = "Thank", y = "You"; 
                                     
        // + and - operator 
        System.out.println("a + b = " + (a + b)); 
        System.out.println("a - b = " + (a - b)); 
                                   
        // + operator if used with strings 
        // concatenates the given strings. 
        System.out.println("x + y = " + x + y);
  
        // * and / operator 
        System.out.println("a * b = " + (a * b)); 
        System.out.println("a / b = " + (a / b)); 
  
        // modulo operator gives remainder 
        // on dividing first operand with second 
        System.out.println("a % b = " + (a % b)); 
  
        // if denominator is 0 in division 
        // then Arithmetic exception is thrown. 
        // uncommenting below line would throw 
        // an exception 
        // System.out.println(a/c); 
        }
    }
        
    
Output:
                a + b = 30
                a - b = 10
                x + y = ThankYou
                a * b = 200
                a / b = 2
                a % b = 0
              
2.Unary Operators

Unary Operators: Unary operators need only one operand. They are used to increment, decrement or negate a value.
1. - : Unary minus, used for negating the values.
2. + : Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive.
3. ++ : Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.
4. Post-Increment : Value is first used for computing the result and then incremented.
5. Pre-Increment : Value is incremented first and then result is computed.
4. -- : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.
5. Post-decrement : Value is first used for computing the result and then decremented.
4. Pre-Decrement : Value is decremented first and then result is computed.
5. ! :Logical not operator, used for inverting a boolean value.

EXAMPLE:


// Java program to illustrate 
// unary operators 
public class operators { 
    public static void main(String[] args) 
    { 
        int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30; 
        boolean condition = true; 
  
        // pre-increment operator 
        // a = a+1 and then c = a; 
          c = ++a; 
          System.out.println("Value of c (++a) = " + c); 
   
                                   
        // post increment operator 
        // c=b then b=b+1 
          c = b++; 
          System.out.println("Value of c (b++) = " + c); 
  
        // pre-decrement operator 
        // d=d-1 then c=d 
          c = --d; 
          System.out.println("Value of c (--d) = " + c); 
  
        // post-decrement operator 
        // c=e then e=e-1 
          c = e--; 
          System.out.println("Value of c (e--) = " + c); 
  
        // Logical not operator 
          System.out.println("Value of !condition =" + !condition); 
        }
    }
        
Output:
            Value of c (++a) = 21
            Value of c (b++) = 10
            Value of c (--d) = 19
            Value of c (e--) = 40
            Value of !condition =false
              
3.Assignment Operator

Assignment Operator : ‘=’ Assignment operator is used to assign a value to any variable. It has a right to left associativity, i.e value given on right hand side of operator is assigned to the variable on the left and therefore right hand side value must be declared before using it or should be a constant.
+= , for adding left operand with right operand and then assigning it to variable on the left.
-= , for subtracting left operand with right operand and then assigning it to variable on the left.
*= , for multiplying left operand with right operand and then assigning it to variable on the left.
/= , for dividing left operand with right operand and then assigning it to variable on the left.
%= , for assigning modulo of left operand with right operand and then assigning it to variable on the left.

EXAMPLE:


// Java program to illustrate 
// assignment operators 
    public class operators { 
        public static void main(String[] args) 
        { 
            int a = 20, b = 10, c; 
  
            // simple assignment operator 
            c = b; 
            System.out.println("Value of c = " + c);
        }
    }
        
Output:
        Value of c = 10
              
4.Relational Operator

Relational Operator : The equality and relational operators determine the relationship between the two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it is evaluated to either true or false.
== , Equal to : returns true of left hand side is equal to right hand side.
!= , Not Equal to : returns true of left hand side is not equal to right hand side.
< , less than : returns true of left hand side is less than right hand side.
<= , less than or equal to : returns true of left hand side is less than or equal to right hand side.
> , Greater than : returns true of left hand side is greater than right hand side..
>= ,Greater than or equal to: returns true of left hand side is greater than or equal to right hand side.

EXAMPLE:


// Java program to illustrate 
// relational operators 
public class operators { 
    public static void main(String[] args) 
    { 
        int a = 30, b = 20; 
        
        // various conditional operators 
        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("a >= b :" + (a >= b)); 
        System.out.println("a != b :" + (a != b)); 
    }
Output:
    a == b :false
    a < b :false
    a <= b :false
    a > b :true
    a >= b :true
    a != b :true
              
5.Logical Operators

Logical Operators :These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision.
Conditional operators are-
|| Logical OR : returns true if at least one condition is true.
&& Logical AND : returns true when both conditions are true.

EXAMPLE:


// Java program to illustrate 
// Logica operators 
public class operators { 
    public static void main(String[] args) 
    { 
        int number1 = 1, number2 = 2, number3 = 9;
        boolean result;
        
        // At least one expression needs to be true for the result to be true
        result = (number1 > number2) || (number3 > number1);

        // result will be true because (number1 > number2) is true
        System.out.println(result);
    			
        // All expression must be true from result to be true	
        result = (number1 > number2) && (number3 > number1);

        // result will be false because (number3 > number1) is false
        System.out.println(result);
    }
}
  
Output:
      true
      false 
              
6.Ternary operator

Ternary operator : Ternary operator is a shorthand version of if-else statement. It has three operands and hence the name ternary. General format is-
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’ else execute the statements after the ‘:’

EXAMPLE:


// Java program to illustrate 
// Ternary operator 
public class operators { 
    public static void main(String[] args) 
    { 
        int a = 20, b = 10, c = 30, result; 
  
        // result holds max of three 
        // numbers 
        result = ((a > b) 
              ? (a > c) 
              ? a 
              : c 
              : (b > c) 
              ? b 
              : c); 
        System.out.println("Max of three numbers =" + result); 
    
    }
}
  
Output:
        Max of three numbers = 30

              
7.Bitwise Operators

Bitwise Operators :These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of Binary indexed tree.
&, Bitwise AND operator: returns bit by bit AND of input values.
|, Bitwise OR operator: returns bit by bit OR of input values.
^, Bitwise XOR operator: returns bit by bit XOR of input values.
~, Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed.

EXAMPLE:


// Java program to illustrate 
// bitwise operators 
public class operators { 
    public static void main(String[] args) 
    { 
        int a = 0x0005; 
        int b = 0x0007; 
  
        // bitwise and 
        // 0101 & 0111=0101 
        System.out.println("a&b = " + (a & b));
    }
}
  
        
Output:
    a&b = 5

              
8.Shift Operators

Shift Operators :These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two. General format-
number shift_op number_of_places_to_shift;
<< Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
>> Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
>>> Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0.

EXAMPLE:


// Java program to illustrate 
// Shift Operators 
public class operators { 
    public static void main(String[] args) 
    { 
        int a = 0x0005; 
        int b = -10; 
  
        // left shift operator 
        // 0000 0101>>2 =0001 0100(20) 
        // similar to 5*(2^2) 
        System.out.println("a>>2 = " + (a >> 2)); 
    }
}
  
        
Output:
    a << 2 = 20
    
              
9.Instance of operator

Instance of operator :Instance of operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass or an interface. General format-
object instance of class/subclass/interface

EXAMPLE:


// Java program to illustrate 
// Instance of operator 
public class operators { 
    public static void main(String[] args) 
    { 
        String test = "asdf";
        boolean result;
    	
        result = test instanceof String;
        System.out.println("Is test an object of String? " + result);
    
    }
}
Output:
    Is test an object of String? true