04.01. Arithmetic Operators

 Arithmetic Operators

  • Eclipse: Oxygen
  • Java: 1.8

Java provides a rich set of operators to manipulate a variable. Operators are the symbols in Java that perform a specific operation on many operands and returns the output. Before knowing the arithmetic operators, let us know the type of operator.

Java operators are divided into the following groups:

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Logical Operators
  • Relational Operators
  • Operators

Arithmetic operator

In this tutorial, we will discuss the arithmetic operator. Mathematical operations are done by using arithmetic operators. Following table displays all the arithmetic operators supported by java. Assume integer variable “A” holds value 5 and variable “B” holds value 10.

Operator Description Example Type Of Operation
+ Add two operands A+B will give 15 Binary
Subtract the second operand from first A-B will give -5 Binary
* Multiply both operands A*B will give 50 Binary
/ Divide numerator by de-numerator B/A will give 2 Binary
% Modulus operator gives a reminder after an integer division B%A will give 0 Binary
++ Increment operator increases integer value by  one A++ will give 6 Unary
Decrement operator decreases integer value by one A—will give 4 Unary

Addition Operator

Plus (+) operator is used for addition of two numbers. In addition to adding two numbers, the addition operator is also used for string concatenation.

The following code demonstrates the addition of two numbers as well as concatenation operation.

Output

3

Hello world

Subtraction operator

Minus (-) operator is used for subtracting the second operand from the first.

Output

11

Multiplication Operator

Multiplication (*) operator used for multiply two numbers.

In following illustration 12*2 gives 24.

Output

24

Division Operator

Division operator (/) used for divide numerator by de-numerator and it gives the quotient.

In the following illustration 12 divided by 2 and the result is 6.

Output

6

Modulus operator

Modulus operator used to returns the remainder of one number divided by another.

In following illustration 13%2 and remainder are 1.

Output

1

Increment operator

Increment operator is a unary operator that increments its operand by one. This operator can be used for both postfix(x++) as well as a prefix (++x).

Postfix-In the postfix form, it evaluates to the value of the operand before the increment operation.

Case 1

Output

10

Prefix-In the prefix form it evaluates the value of the operand after the increment operation.

Case 2

Output

11

In case 1, the value got printed first and the value incremented, whereas in case 2, the value of x is incremented first and then the value 11 got printed. So do you now understood the difference?

In prefix, the value of the variable will get incremented first and then the statement will get executed. Whereas in postfix, the statement will be executed with the current value of the variable and then the value gets incremented

Decrement operator:

Decrement operator is also a unary operator that decrements its operand by one. This operator can be used for both postfix(x–) as well as a prefix (–x).

Postfix-In the postfix form, it evaluates to the value of the operand before the decrement operation.

Case 1

Output

10

Prefix-In the prefix form it evaluates the value of the operand after the increment operation.

Case 2

Output

9

In case 1, the value got printed first and then the value decremented, whereas in case 2, the value of x is decremented first and then the value 9 got printed.

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments