Java Arithmetic Operators with Examples

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:

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

This article explains all that one needs to know regarding the Arithmetic Operators.

Arithmetic Operators

These operators involve the mathematical operators that can be used to perform various simple or advance arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands respectively. Let’s look at the various operators that Java has to provide under the arithmetic operators.

Now let’s look at each one of the arithmetic operators in Java:



  1. Addition(+): This operator is a binary operator and is used to add two operands.

    Syntax:

    num1 + num2

    Example:

    num1 = 10, num2 = 20
    sum = num1 + num2 = 30
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate Addition operator
      
    import java.io.*;
      
    class Addition {
        public static void main(String[] args)
        {
            // initializing variables
            int num1 = 10, num2 = 20, sum = 0;
      
            // Displaying num1 and num2
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);
      
            // adding num1 and num2
            sum = num1 + num2;
            System.out.println("The sum = " + sum);
        }
    }

    chevron_right

    
    

    Output:

    num1 = 10
    num2 = 20
    The sum = 30
    
  2. Subtraction(-): This operator is a binary operator and is used to subtract two operands.
    Syntax:

    num1 - num2

    Example:

    num1 = 20, num2 = 10
    sub = num1 - num2 = 10
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate Subtraction operator
      
    import java.io.*;
      
    class Subtraction {
        public static void main(String[] args)
        {
            // initializing variables
            int num1 = 20, num2 = 10, sub = 0;
      
            // Displaying num1 and num2
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);
      
            // subtracting num1 and num2
            sub = num1 - num2;
            System.out.println("Subtraction = " + sub);
        }
    }

    chevron_right

    
    

    Output:

    num1 = 20
    num2 = 10
    Subtraction = 10
    
  3. Multiplication(*): This operator is a binary operator and is used to multiply two operands.
    Syntax:

    num1 * num2

    Example:



    num1 = 20, num2 = 10
    mult = num1 * num2 = 200
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate Multiplication operator
      
    import java.io.*;
      
    class Multiplication {
        public static void main(String[] args)
        {
            // initializing variables
            int num1 = 20, num2 = 10, mult = 0;
      
            // Displaying num1 and num2
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);
      
            // Multiplying num1 and num2
            mult = num1 * num2;
            System.out.println("Multiplication = " + mult);
        }
    }

    chevron_right

    
    

    Output:

    num1 = 20
    num2 = 10
    Multiplication = 200
    
  4. Division(/): This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as result.
    Syntax:

    num1 / num2

    Example:

    num1 = 20, num2 = 10
    div = num1 / num2 = 2
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate Division operator
      
    import java.io.*;
      
    class Division {
        public static void main(String[] args)
        {
            // initializing variables
            int num1 = 20, num2 = 10, div = 0;
      
            // Displaying num1 and num2
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);
      
            // Dividing num1 and num2
            div = num1 / num2;
            System.out.println("Division = " + div);
        }
    }

    chevron_right

    
    

    Output:

    num1 = 20
    num2 = 10
    Division = 2
    
  5. Modulus(%): This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor).
    Syntax:

    num1 % num2

    Example:

    num1 = 5, num2 = 2
    mod = num1 % num2 = 1
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate Modulus operator
      
    import java.io.*;
      
    class Modulus {
        public static void main(String[] args)
        {
            // initializing variables
            int num1 = 5, num2 = 2, mod = 0;
      
            // Displaying num1 and num2
            System.out.println("num1 = " + num1);
            System.out.println("num2 = " + num2);
      
            // Remaindering num1 and num2
            mod = num1 % num2;
            System.out.println("Remainder = " + mod);
        }
    }

    chevron_right

    
    

    Output:

    num1 = 5
    num2 = 2
    Remainder = 1
    
  6. Increment(++): This is a unary operator that acts on one operand, unlike the previous operations. It is used to increment the value of an integer. It can be used in two ways:
    1. Post-increment operator: When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
      Syntax:

      num++

      Example:

      num = 5
      num++ = 6
    2. Pre-increment operator: When placed before the variable name, the operand’s value is incremented instantly.
      Syntax:



      ++num

      Example:

      num = 5
      ++num = 6
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate increment operator
      
    import java.io.*;
      
    class Increment {
        public static void main(String[] args)
        {
            // initializing variables
            int num = 5;
      
            // first 5 gets printed and then
            // increment to 6
            System.out.println("Post "
                               + "increment = " + num++);
      
            // num was 6, incremented to 7
            // then printed
            System.out.println("Pre "
                               + "increment = " + ++num);
        }
    }

    chevron_right

    
    

    Output:

    Post increment = 5
    Pre increment = 7
    
  7. Decrement(–): This is also a unary operator that acts on one operand. It is used to decrement the value of an integer. It can be used in two ways:
    1. Post-decrement operator: When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
      Syntax:

      num--

      Example:

      num = 5
      num-- = 4
    2. Pre-decrement operator: When placed before the variable name, the operand’s value is decremented instantly.
      Syntax:

      --num

      Example:

      num = 5
      --num = 4
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate decrement operator
      
    import java.io.*;
      
    class Decrement {
        public static void main(String[] args)
        {
            // initializing variables
            int num = 5;
      
            // first 5 gets printed and then
            // decremented to 4
            System.out.println("Post "
                               + "decrement = " + num--);
      
            System.out.println("num = " + num);
      
            // num was 4, decremented to 3
            // then printed
            System.out.println("Pre "
                               + "decrement = " + --num);
        }
    }

    chevron_right

    
    

    Output:

    Post decrement = 5
    num = 4
    Pre decrement = 3
    

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.