Arithmetic Operators in Java

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Arithmetic Operators

Overview

Arithmetic operators in Java are used to perform the various basic and advanced-level mathematics operations, which can be performed on data-type variables that can be referred to as operands. Java programming language supports many arithmetic unary and binary operators like Addition, Subtraction, Multiplication, Division, Modulus, and Increment/Decrement operator.

Introduction to Arithmetic Operators in Java

About 1.5 million years ago, humans invented symbolic language to represent concepts using special characters and images. Since then, we have been using several symbols to refer to something. For instance, how easily we can identify adding or mixing something just by looking at the plus sign. Not only humans but machines also understand these symbolic words, which are called Operators in technical terminology.

If we talk about Computer programming languages, all have basic building blocks formed by Operators. Like others, Java also provides various types of operators to perform a specific operation. In this article, we will discuss Arithmetic Operators in Java.

Arithmetic Operators in Java

Java arithmetic operators are nothing but mathematical operators, which are used to implement simple or advanced-level calculations on all the primitive data types including integers, floats, and even characters (except the boolean data type).

In the case of Strings, we can perform only the addition operation, which consists of two strings. Also, if we talk about user-defined data types then there might be a chance we can use one or more arithmetic operators.

In Java, an arithmetic operator can be divided into two categories listed below -

  1. Binary Operators - The operators which require two operands to implement the action/operation are known as Binary operators. Here is the list of binary operators in Java -
Operator NameOperatorDescription
Addition Operator+Addition of two numbers
Subtraction Operator-Subtraction of two numbers
Multiplication Operator*Multiplication of two numbers
Division Operator/Division of two numbers
Modulus Operator%Return remainder after dividing two numbers
  1. Unary Operators - The operators who perform the operation on a single variable and provide results are known as Unary Operators. Here is the list of unary operators in Java -
Operator NameOperatorDescription
Increment Operator++Increase the value by 1
Decrement Operator- -Decrease the value by 1

Now we will discuss each Arithematic Operators in Java in detail.

Binary Operators

1. Addition Operator (+)

The addition operator is a binary operator as it is used to perform the addition of two operands. (Operand is a primitive data type variable) Syntax -

Example

Moreover,

  • (+) operator can also be used to do the concatenation of two strings. Using this operator we can add two strings like - ABC + DE = ABCDE .

    • Not only strings but, if we add any other primitive data types with strings then it will show the result in string format only. Like -
  • The binary addition operator can also be used as a Unary operator.

    • We can use this Unary operator with primitive data types including int, float, double, and char.
    • It always increments by one. So if we try to use it with a double value of 5.6, it will show the result as 6.6.
    • While working with the Character data type, it will increase the ASCII value of that character. So if we use this unary operator on z (ASCII-122), then it will show { (ASCII-123).
  • In addition, Java also enables us to use the addition operator on the char data type.

    • If we use an addition operator with a char type variable then it will perform addition on their ASCII values. a + b = 195 (where ASCII of a is 97 and ASCII of b is 98)

Let's understand the addition operator with a small Java program. In the program, we'll first create two variables of type integer, and add them to the result variable.

Output -

In the above code, we defined two variables num1 and num2 with the values 10 and 20 respectively. By using the addition operator, we added both integer type variables and stored the generated result which is 30 (10+20) in the result variable.

2. Subtraction Operator (-)

The subtraction operator implements the subtraction operation as we do in mathematics. This binary operator can only deal with numeric values. Syntax -

Example

Moreover -

  • It can also be used as a unary operator which returns the negative value of a variable.
    • Like the addition unary operator, this also can be used with primitive data types including int, float, double, and char.
    • It always decrements by one. So if we try to use it with a double value of 5.6, it will show a result of 4.6.
    • While working with the Character data type, it will decrease the ASCII value of that character. So if we use this unary operator on z (ASCII-122), then it will show y (ASCII-121).
  • Like the Addition operator, Java also allows us to implement (-) operators on char data-typed operands.
    • If we use the subtraction operator with char type variable then it will perform subtraction on their ASCII values. a - b = -1 (where ASCII of a is 97 and ASCII of b is 98)

Let's understand the subtraction operator with a small Java program.

Output -

In the above code, we defined two variables num1 and num2 with the values 15 and 10 respectively. By using the subtraction operator, we subtract num2 from num1 integer type variable and stored the generated result which is 5 (15-10) in the result variable.

3. Multiplication Operator (*)

As we do in mathematics, the Multiplication operator in Java is used to implement the multiplication of two operands. This binary operator executes the basic and advanced level mathematic multiplication.

The multiplication operator can be used with int, float, double, and also with char. But with the char data type, multiplication is performed with the character's ASCII values. Syntax -

Example

Let's understand multiplication operator with small Java program.

Output -

In the above code, we defined two variables num1 and num2 with the values 5 and 3 respectively. By using the multiplication operator, we multiply them and stored the generated result which is 15 (5*3) in the result variable.

4. Division Operator (/)

The division operator is a binary operator which uses two operands (dividend and divisor) and gives the result (quotient) by dividing them. We can use division operators with primitive data types int, float, double, also char, wherewith char division is performed on their ASCII value.

If both dividend and divisor are a type of integer then the quotient is also an integer. Moreover, if any of them has a data type of float or double then the result will have a decimal value.

Syntax -

Example

Let's understand division operators with a small Java program.

**Output **

In the above code, we defined two variables num1 and num2 with the values 15 and 3 respectively. By using the division operator, we divide num1 by num2 and store the generated result which is 5 (15/3) in the result variable.

Division Operator also causes one type of exception. When we are dividing any value with 0 (zero) the system throws a DivideByZeroException, which is a type of an Arithmetic Exception.

Output -

5. Modulus Operator (%)

In Java's arithmetic operator, modulo is a very helpful operator. This binary provides the remainder when the dividend is divided by the divisor.

We can use Modulus operators with primitive data types: int, float, double, also char. Moreover, with a negative value also modulo works the same except its shows a negative result.

Syntax -

Example

Though, the modulo works differently with each data type.

  • Shows integer remainder with int variable i.e. 10%3 = 1
  • With float, it shows remainder in float decimal i.e. 10.0%3 = 1.0
  • It shows remainder with decimal with double variable i.e. 6.5%3 = 0.5
  • With char, the operation is performed on the operator's ASCII values i.e. 'z'%3 = 2
  • Modulo with negative value shows negative remainder i.e. -10%3 = -1

Let's understand the modulus operator with a small Java program.

Output -

In the above code, we defined two variables num1 and num2 with the values 10 and 3 respectively. We performed the modulo operator on num1 and num2 and stored the generated result which is 1 (10%3) in the result variable.

Like the Divison operator, Modulus also throws an ArithmaticException and returns NaN when we take the modulo of any value with 0.

Output -

Note: In Java, we can use the modulo operator for floating-point values, which cannot be possible in C and C++ languages.

Unary Operators

1. Increment Operator (++)

Unlike the above-discussed operators, this is a unary operator, that can use on a single operand. The increment operator increases the variable value by 1.

Moreover, it can be achieved using two different ways:

  1. Post Increment Operator: When the increment operator is placed after the operands, the actual value of the variable is used first, and then it gets incremented.
    It's like we took something and used it but, before giving it to anyone we added or modified it. Mostly, we use the post-increment operator when we need to execute an expression with the previous value first and then need to update the value.

Syntax-

Example

  1. Pre Increment Operator: When the increment operator is placed before the operands, the value of the variable increment is first and then used.

    Mostly, we use the pre-increment operator when we need to update the value first and then need to execute the expression with the latest value.

Syntax

Example

Let's understand both type of increment operator with small Java program.

Output -

In the above code, we defined a variable called i with 0 value assigned to it. When we try to print (i), first it prints the initial value (0) and then the value remains the same as the value of i remains the same while printing and increments after it. On the other hand, by implementing (++j) we increment the value of j from 0 to 1 before printing.

2. Decrement Operator (--)

A decrement operator is a unary operator which acts on one operand. This operator decreases the integer value by 1 at a time.

Like the Increment operator, the decrement operator also can be used in two different ways:

  1. Post Decrement Operator: When the decrement operator is placed after the operands, the actual value of the variable is used first, and then it gets decremented.

    Mostly, we use the post-decrement operator when we need to execute an expression with the previous value first and then need to decrease the value.

Syntax

Example

  1. Pre Decrement Operator: When the decrement operator is placed before the operands, the value of the variable decrement is first and then used.

    Mostly, we use pre decrement operator when we need to decrease the value first and then need to execute the expression with the latest value.

Syntax

Example

Let's understand both types of decrement operators with small Java programs.

Output -

In the above code, we defined a variable called i with 2 value assigned to it. When we try to print (i--), first it printed the initial value (2) and then the value got decremented by 1, and 1 is printed at the next line. On the other hand, implementing (--j) decrement the value of j from 2 to 1 before printing.

Java Program to implement Arithmetic Operator

Now, let's use all the Java Arithmetic Operators in the single program for better understanding.

Output -

In the above code, we tried to put all the arithmetic operators in Java together. In this, we created two variables of type int and float. We keep using unary operators to increment and decrement the int variable and perform arithmetic binary operators with float. Also printed the result of every binary operator separately.

Program to implement Arithmetic Operators in Java using Scanner Class

Let's do the above java program by giving runtime value using the Scanner class located in java.util package.

Output -

The above code is the same as the last one except, that here we have used the Scanner class. Scanner class is in util package and used to take input from users. So instead of assigning value directly, we do not take it from the user. You can see that for int and float we used different styles nextInt() and nextFloat() respectively.

Conclusion

  • Arithmetic operators in Java implement basic mathematical operations.
  • There are two types of operators: Binary and Unary, Binary works on two operands whereas unary in a single operand.
  • However, every computer programming language has arithmetic operators, but programmers can find more flexibility in Java. For instance, an addition operator can be used to add two primitive data-typed variables and also for string concatenation. Which even helps to reduce the complexity of code.