Overview of Java Operators


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Java Operators </aside>

<aside> 💡

  1. Precedence and Associativity of Java Operators </aside>

<aside> 💡

  1. Advantages and Disadvantages of Operators </aside>

Java Operators


  1. Java Operators

    1. Java operators are special symbols that perform operations on variables or values. They can be classified into several categories based on their functionality. These operators play a crucial role in performing arithmetic, logical, relational, and bitwise operations etc.

    2. Example: Here, we are using + and  operators.

      // Java program to show the use of + and - operators
      public class SampleProgram
      {  
          public static void main(String[] args) 
          {
        
            // Declare and initialize variables
              int num1 = 500;
              int num2 = 100;
              
              // Using the + (addition) operator
              int sum = num1 + num2;
              System.out.println("The Sum is: "+sum);
              
              // Using the - (subtraction) operator
              int diff = num1 - num2;
              System.out.println("The Difference is: "+diff);
              
          }
      }
      
    3. Output

      The Sum is: 600
      The Difference is: 400
      
    4. Types of Operators in Java

      1. Arithmetic Operators
      2. Unary Operators
      3. Assignment Operator
      4. Relational Operators
      5. Logical Operators
      6. Ternary Operator
      7. Bitwise Operators
      8. Shift Operators
      9. instance of operator

  2. Arithmetic Operators

    1. Arithmetic Operators are used to perform simple arithmetic operations on primitive and non-primitive data types.

      1. : Multiplication
      2. / : Division
      3. % : Modulo
      4. + : Addition
      5. – : Subtraction
    2. Example:

      // Java Program to show the use of
      // Arithmetic Operators
      import java.io.*;
      
      class SimpleProgram
      {
          public static void main (String[] args) 
          {
                
              // Arithmetic operators on integers
              int a = 10;
              int b = 3;
            
              // Arithmetic operators on Strings
              String n1 = "15";
              String n2 = "25";
      
              // Convert Strings to integers
              int a1 = Integer.parseInt(n1);
              int b1 = Integer.parseInt(n2);
                 
              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("a1 + b1 = " + (a1 + b1)); 
                
          }
      }
      
    3. Output

      a + b = 13
      a - b = 7
      a * b = 30
      a / b = 3
      a % b = 1
      a1 + b1 = 40
      

  3. Unary Operators

    1. Unary Operators need only one operand. They are used to increment, decrement, or negate a value.

    2. -, Negates the value.

    3. +, Indicates a positive value (automatically converts bytechar, or short to int).

    4. ++, Increments by 1.

      1. Post-Increment: Uses value first, then increments.
      2. Pre-Increment: Increments first, then uses value.
    5. -, Decrements by 1.

      1. Post-Decrement: Uses value first, then decrements.
      2. Pre-Decrement: Decrements first, then uses value.
    6. !, Inverts a boolean value.

    7. Example:

      // Java Program to show the use of
      // Unary Operators
      import java.io.*;
      
      // Driver Class
      class SampleProgram {
            // main function
          public static void main(String[] args)
          {
              // Interger declared
              int a = 10;
              int b = 10;
      
              // Using unary operators
              System.out.println("Postincrement : " + (a++));
              System.out.println("Preincrement : " + (++a));
      
              System.out.println("Postdecrement : " + (b--));
              System.out.println("Predecrement : " + (--b));
          }
      }
      
    8. Output

      Postincrement : 10
      Preincrement : 12
      Postdecrement : 10
      Predecrement : 8
      

  4. Assignment Operator

    1. ‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e. value given on the right-hand side of the 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.

    2. The general format of the assignment operator is:

      variable = value;
      
    3. In many cases, the assignment operator can be combined with others to create shorthand compound statements. For example, a += 5 replaces a = a + 5. Common compound operators include:

      1. += , Add and assign.
      2. = , Subtract and assign.
      3. = , Multiply and assign.
      4. /= , Divide and assign.
      5. %= , Modulo and assign.
    4. Example:

      // Java Program to show the use of
      // Assignment Operators
      import java.io.*;
      
      // Driver Class
      class SampleProgram {
          // Main Function
          public static void main(String[] args)
          {
              
              // Assignment operators
              int f = 7;
              System.out.println("f += 3: " + (f += 3));
              System.out.println("f -= 2: " + (f -= 2));
              System.out.println("f *= 4: " + (f *= 4));
              System.out.println("f /= 3: " + (f /= 3));
              System.out.println("f %= 2: " + (f %= 2));
              System.out.println("f &= 0b1010: " + (f &= 0b1010));
              System.out.println("f |= 0b1100: " + (f |= 0b1100));
              System.out.println("f ^= 0b1010: " + (f ^= 0b1010));
              System.out.println("f <<= 2: " + (f <<= 2));
              System.out.println("f >>= 1: " + (f >>= 1));
              System.out.println("f >>>= 1: " + (f >>>= 1));
          }
      }
      
    5. Output

      f += 3: 10
      f -= 2: 8
      f *= 4: 32
      f /= 3: 10
      f %= 2: 0
      f &= 0b1010: 0
      f |= 0b1100: 12
      f ^= 0b1010: 6
      f <<= 2: 24
      f >>= 1: 12
      f >>>= 1: 6
      

  5. Relational Operators