Introduction to How Java Operators Work

Introduction

 
This article explains the basic Java operators.
 

What is an Operator?

 
An operator is a method that manipulates values. Let's use the example of addition, in which we add two values (like 1+2) where the values are operands and the "+" sign is the operator, in other words, the operator is a method used to operate on operands.
 

Operators in Java

 
Java provides various types of operators to manipulate various values.
  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators.
  • Logical Operators.
  • Assignment Operators.
  • Miscellaneous Operators.

1. Arithmetic Operators

 
It is used in mathematical expressions in the same way it is used in algebra. It includes the following:
  • Addition (+)
  • Subtraction (-)
  • Manipulation (*)
  • Division (/)
  • Modulus (%)
  • Increment (++)
  • Decrement (--)
Example
 
ArithmeticEx.java
  1. public class ArithmeticEx   
  2. {  
  3.  public static void main(String args[])   
  4.  {  
  5.   int i = 15;  
  6.   int j = 25;  
  7.   int k = 35;  
  8.   int l = 10;  
  9.   System.out.println("i + j = " + (i + j));  
  10.   System.out.println("i - j = " + (i - j));  
  11.   System.out.println("i * j = " + (i * j));  
  12.   System.out.println("j / i = " + (j / i));  
  13.   System.out.println("j % i = " + (j % i));  
  14.   System.out.println("k % i = " + (k % i));  
  15.   System.out.println("i++ = " + (i++));  
  16.   System.out.println("j-- = " + (i--));  
  17.  }  
  18. }  
Output
 
fig-1.jpg
  

2. Relational Operators

 
This operator is used to compare two values.
  • > (Greater Than operator).
  • < (Less Than operator).
  • == (EqualTo operator)
  • != (Not EqualsTo operator; Work same as EqualTo operator).
  • >= (Greater than EuqalTo).
  • <= (Less Than EqualTo).
    Example
     
    RelationalOperatorEx.java
    1. public class RelationalOperatorEx  
    2. {  
    3.  public static void main(String args[])   
    4.  {  
    5.   int i = 10;  
    6.   int j = 20;  
    7.   System.out.println("i == j = " + (i == j));  
    8.   System.out.println("i != j = " + (i != j));  
    9.   System.out.println("i > j = " + (i > j));  
    10.   System.out.println("i < j = " + (i < j));  
    11.   System.out.println("j >= i = " + (j >= i));  
    12.   System.out.println("j <= i = " + (j <= i));  
    13.  }  
    14. }   
    Output
    Fig-2.jpg
     

    3. Bitwise Operator

     
    This operator works on bits to perform bit by bit operations.
    • >>> (Shift right zero fill operaotr).
    • << (Binary Left Shift Operator).
    • >> (binary right shift operator)
    • && (Binary and operator)
    • || (Binary Or).
    • ^ (Binary XOR).
    • ~ (Binary Ones Complement; used to flipping bits).
      Example
      1. BitwiseOperatorEx.java  
      2. public class BitwiseOperatorEx   
      3. {  
      4.  public static void main(String args[])   
      5.  {  
      6.   int i = 45;  
      7.   int l = 15;  
      8.   int k = 0;  
      9.   k = i & l;  
      10.   System.out.println("i & l = " + k);  
      11.   k = i | l;  
      12.   System.out.println("i | l = " + k);  
      13.   k = i ^ l;  
      14.   System.out.println("i ^ l = " + k);  
      15.   k = ~i;  
      16.   System.out.println("~i = " + k);  
      17.   k = i << 2;  
      18.   System.out.println("i << 2 = " + k);  
      19.   k = i >> 2;  
      20.   System.out.println("i >> 2 = " + k);  
      21.   k = i >>> 2;  
      22.   System.out.println("i >>> 2 = " + k);  
      23.  }  
      24. }   
      Output
      fig-3.jpg
       

      4. Logical Operators

       
      This operator works on Boolean conditions (like true/false conditions).
      • || (Logical Or)
                True if any of the two operands are positive.
      • && (Logical And operator)
                True if both the operands are positive.
      • ! ( Logical Not Operator)
                If a condition is true then the logical Not operator will return false.
        Example
         
        LogicalOperator.java
        1. public class LogicalOperatorEx   
        2. {  
        3.  public static void main(String args[])   
        4.  {  
        5.   boolean i = true;  
        6.   boolean j = false;  
        7.   System.out.println("i || j = " + (i || j));  
        8.   System.out.println("i && j = " + (i && j));  
        9.   System.out.println("!(i && j) = " + !(i && j));  
        10.  }  
        11. }   
        Output
        fig-4.jpg
         

        5. Assignment Operator

         
        The following are the assignment operators provided by Java.
        • *= (Multiply assignment operator)
        • /= (Divide assignment operator)
        • |= (bitwise inclusive OR assignment operator).
        • = ( Simple assignment operator)
        • >>= ( Right shift assignment operator)
        • += ( Add assignment operator)
        • -= ( Subtract assignment operator)
        • %= (Modulus assignment operator)
        • <<= (Left shift assignment operator).
        • &=  (Bitwise assignment operator).
        • ^= ( bitwise exclusive OR assignment operator).
        Example
         
        AssignmentOperatorEx.java
        1. public class AssignmentOperatorEx   
        2. {  
        3.  public static void main(String args[])   
        4.  {  
        5.   int i = 15;  
        6.   int j = 25;  
        7.   int k = 0;  
        8.   k = i + j;  
        9.   System.out.println("k = i + j = " + k);  
        10.   k += i;  
        11.   System.out.println("k += i  = " + k);  
        12.   k -= i;  
        13.   System.out.println("k -= i = " + k);  
        14.   k *= i;  
        15.   System.out.println("k *= i = " + k);  
        16.   i = 15;  
        17.   k = 15;  
        18.   k /= i;  
        19.   System.out.println("k /= i = " + k);  
        20.   i = 15;  
        21.   k = 15;  
        22.   k %= i;  
        23.   System.out.println("k %= i  = " + k);  
        24.   k <<= 2;  
        25.   System.out.println("k <<= 2 = " + k);  
        26.   k >>= 2;  
        27.   System.out.println("k >>= 2 = " + k);  
        28.   k >>= 2;  
        29.   System.out.println("k >>= i = " + k);  
        30.   k &= i;  
        31.   System.out.println("k &= 2  = " + k);  
        32.   k ^= i;  
        33.   System.out.println("k ^= i   = " + k);  
        34.   k |= i;  
        35.   System.out.println("k |= i   = " + k);  
        36.  }  
        37. }   
        Output
         
        fig-5.jpg


        Similar Articles