Java Arithmetic Operators

This article explains the arithmetic operators in Java.
 

Arithmetic Operators

 

These operators perform fundamental arithmetic operations like addition, subtraction, multiplication, division etc.

 

The operands for arithmetic operators must be numeric. For example, these can’t be used on boolean types. These can be used on char types though, since char is subset of int.

 

These can be binary or unary operators depending on the number of operands.

 

Binary operators

 

These operators act on two operands at a time.

Here is a list :

OperatorMeaningExampleResult
+Addition operator2+35
Subtraction operator5-32
*Multiplication operator2*36
/Division operator4/22
%Modulus or remainder operator12%52

 

Here is an example :

 

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    int x = 10, y =2;
    int add = x + y;
    System.out.println(add);
    int subtract = x - y;
    System.out.println(subtract);
    int multiply = x * y;
    System.out.println(multiply);
    int divide = x / y;
    System.out.println(divide);
    int mod = x % y;
    System.out.println(mod);
  }

Here is the output of above program :

12

8

20

5

0

 

Unary operators

 

These operators act on only one operand.

 

These include :

  • Unary plus (+) and minus (-) operators
  • Increment operator (++)
  • Decrement operator (–)

 

The unary minus operator negates the operand value. The unary plus operator does not change the operand value.

 

The increment operator ++ increase the value by 1.

 

When ++ is used before a variable, its pre-increment and when ++ is added after a variable its post-increment.

 

In pre-increment, the value of the variable is incremented first and rest operations are performed later whereas in post-increment, the variable is incremented last.

 

Here is an example of pre-increment:

 

    int x =1;
    System.out.println(x);
    System.out.println(++x);
    System.out.println(x);

This will print :

1

2

2

 

Now, refer the below example for how post-increment will behave in this scenario:

 

int x =1;
    System.out.println(x);
    System.out.println(x++);
    System.out.println(x);

The output will be :

1

1

2

 

The decrement operator — decreases the operand value by 1.

Similar to increment operator, decrement could be pre-decrement or post-decrement depending on whether – is added before the variable or after the variable.

 

Compound Assignment Operators

 

While using assignment, sometimes we have to use the same variable on both sides of assignment.

For example, x = x+1

 

For these scenarios, java provides special operators that can be used to combine an arithmetic operation with an assignment.

For example, x = x+1 can be written as :

x += 1

 

Here, += is the compound assignment operator.

Here is the list of the compound assignment operators:

OperatorResult
+=Addition assignment
-=Subtraction assignment
*=Multiplication assignment
/=Division assignment
%=Modulus assignment

 

 

© 2015, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags