What are the arithmetic operators in Java?



This article will help you understand all about Arithmetic Operators in Java. Before jumping into Arithmetic Operators, let us revise about Operators.

OPERATORS

Programming in Computer Science requires some arithmetical or logical operations. In such circumstances, we need operators to perform these tasks. Thus, an Operator is basically a symbol or token, which performs arithmetical or logical operations and gives us meaningful result. The values involved in the operation are called Operands.

Here is a basic Pictorial representation of Operators.

Now, let us discuss the types of Operators available.

TYPES OF OPERATORS

There are three types of Operators in Java which are −

  • Arithmetical Operators

  • Relational Operators

  • Logical Operators

In this article we are only discussing about Arithmetical Operators, so let’s jump into it.

ARITHMETICAL OPERATORS

The operators which are applied to perform arithmetical calculations in a program, are known as Arithmetical operators. Some basic calculations like addition, subtraction, multiplication, division, modulus are often used during programming. One can apply arithmetic operators like +, -, *, / and % respectively to carry out these calculations. For example, A + B, A – B, A % B are all examples of arithmetical operator usage.

Do you know what Arithmetical Expressions are? A + B is considered an Arithmetical Expression. Basically, an Arithmetical expression may contain variables, constants and arithmetical operators together to produce a meaningful result. A + B, A – B, A % B are all examples of arithmetical expressions.

Now what happens if we assign an arithmetical expression to a variable? For example, what do we call X = A + B? Is it an arithmetical expression or is it something else? Well this is called an Arithmetical Statement. If an arithmetical expression is assigned to a variable then it is known as Arithmetical Statement. Syntax of Arithmetical Statement is − X = A + B, Y = A % B etc.

Now we need to know who to write expression in Java Program.

EXPRESSIONS IN JAVA

When you write a program in Java, it is necessary to represent the arithmetical expressions into Java expressions. Given below are few examples of how to illustrate a mathematical expression in JAVA.

  • Mathematical Expression: abc

    Java Expression: a * b * c

  • Mathematical Expression: ab + bc + ca

    Java Expression: a * b + b * c + c * a

  • Mathematical Expression: a2 + b2

    Java Expression: a * a + b * b

  • Mathematical Expression: (1/3)ab + (2/5)ba

    Java Expression: 1.0/3.0 * a * b + 2.0/5.0 * b * a

Now there exist three different types of Arithmetical Operators. They are −

  • Unary Operator

  • Binary Operator

  • Ternary Operator

Let us understand each of them individually.

UNARY OPERATOR

An Arithmetical operator, which is applied with a single operand is known as Unary Operator. Example: +, -, ++, --, etc. Details of each types of operators are given below.

Unary (+) Operator

This operator is applied before the operand. It is just applied as a pointer to a variable which results in the same value of a variable. Example: If a = 10, then +a will result in 10. If a = -10, then +a will result in -10.

Example

import java.io.*; // importing java.io package public class UnaryTest { // class declaration public static void main(String[] args) { // main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number number = +number; // applying unary (+) operator System.out.println("Updated Number = "+number); // displaying number after applying unary (+) operator } }

Unary (-) Operator

This operator is applied before the operand, same as that of Unary (+) operator. Unary (-) reverts the sign of an operand. Example: If a = 10, then –a will result in -10. If a = 0, then –a will result in 0. If a = -10, then –a will result in 10.

Example

import java.io.*; // importing java.io package public class UnaryTest {// class declaration public static void main(String[] args) {// main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number number = -number; // applying unary (-) operator System.out.println("Updated Number = "+number); // displaying number //after applying unary (-) operator; } }

Unary (++) Operator

This operator increases the value of an operand by one. Example: a = a + 1, by applying increment operator it can be written as a++ or ++a.

Example

import java.io.*; // importing java.io package public class UnaryTest { // class declaration public static void main(String[] args){ // main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number number = ++number; // applying unary (++) operator System.out.println("Updated Number = "+number); // displaying number //after applying unary (++) operator } }

Unary (--) Operator

This operator decreases the value of an operand by one. Example: a = a - 1, by applying decrement operator it can be written as a-- or --a.

Example

import java.io.*; // importing java.io package public class UnaryTest { // class declaration public static void main(String[] args) { // main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number number = --number; // applying unary (--) operator System.out.println("Updated Number = "+number); // displaying number //after applying unary (--) operator } }

In part (iii) of Unary Operator, we see that both a++ and ++a will increment value of a by 1. Then what’s the difference between the two. Let’s find out.

Prefix Operator (++a)

When increment or decrement operators are applied before the operant, it is known as prefix operators. This operator works on the principle “CHANGE BEFORE ACTION”. It means the value of the variable changes before the operation takes place.

Example − Increment Prefix Operator

a = 10;
a = ++a * 2;

Before the operation, value of a was 10. Then in the next arithmetical statement, ++a increases a’s value by 1. So the new value of a is 11. Then the arithmetical operation a * 2 takes place, after which value of a becomes 22.

Example: Decrement Prefix Operator

a = 10;
a = --a * 2;

Before the operation, value of a was 10. Then in the next arithmetical statement, --a decreases a’s value by 1. So the new value of a is 9. Then the arithmetical operation a * 2 takes place, after which value of a becomes 18.

Code − Prefix increment and decrement operator

import java.io.*; // importing java.io package public class PrefixTest { // class declaration public static void main(String[] args) { // main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number ++number; // applying increment perfix operator System.out.println("Updated Number after increment = "+number); // displaying number after increment --number; // applying decrement prefix operator System.out.println("Updated Number after decrement = "+number); // displaying number after decrement } }

Postfix Operator (a++)

When increment or decrement operators are applied after the operant, it is known as postfix operators. This operator works on the principle “CHANGE AFTER THE ACTION”. It means the value of the variable changes after the operation takes place.

Example − Increment Postfix Operator

a = 10;
a = a++ * 2;

Before the operation, value of a was 10. Then the arithmetical operator a * 2 takes place, after which value of a become 20. This value is then stored in a. Hence the new value of a becomes 20.

Example − Decrement Postfix Operator

a = 10;
a = a-- * 2;

Before the operation, value of a was 10. Then the arithmetical operator a * 2 takes place, after which value of a become 20. This value is then stored in a. Hence the new value of a becomes 20.

Code − Postfix increment and decrement operator

import java.io.*; // importing java.io package public class PostfixTest { // class declaration public static void main(String[] args){ // main function declaration int number = 10; // initializing variable System.out.println("Original Number = "+number); // displaying original number number++; // applying increment postfix operator System.out.println("Updated Number after increment = "+number); // displaying number after increment number--; // applying decrement postfix operator System.out.println("Updated Number after decrement = "+number); // displaying number after decrement } }

BINARY OPERATOR

An arithmetic operator which deals with two operands, is known as Binary Arithmetic Operator.

Example

Addition (+): a = 10, b = 7, a + b = 17
Subtraction (-): a = 10, b = 7, a – b = 3
Multiplication (*): a = 10, b = 7, a * b = 70
Division (/): a = 10, b = 7, a / b = 1
Modulus (&): a = 10, b = 7, a & b = 3

Code − Addition, Subtraction, Multiplication, Division and Modulus

import java.io.*; // importing java.io package public class BinaryTest { // class declaration public static void main(String[] args) { // main function declaration int a = 10, b = 7; // variable initialization System.out.println("Value of a = "+a); // displaying value of a and b System.out.println("Value of b = "+b); int sum = a + b; // calculating addition System.out.println("a + b = "+sum); int sub = a - b; // calculating subtraction System.out.println("a - b = "+sub); int mul = a * b; // calculating multiplication System.out.println("a * b = "+mul); int div = a / b; // calculating division System.out.println("a / b = "+div); int mod = a % b; // calculating modulus System.out.println("a % b = "+mod); } }

TERNARY OPERATOR

Ternary Operators deal with three operands. It is also called conditional assignment statement because the value assigned to a variable depends upon a logical expression.

Syntax: variable = (test expression)? Expression 1: Expression 2

Example: a = 10, b = 7;

Max = (a>b)? a:b;

Here, the value 10 is stored in Max as a>b is true.

Min = (b>a)? a:b;

Here, the value 7 is stored in Min as b>a is false.

Code: Ternary Operator Example

import java.io.*; // importing java.io package public class TernaryTest { // class declaration public static void main(String[] args) { // main function declaration int a = 10, b = 7; // variable initialization System.out.println("Value of a = "+a); // displaying value of a and b System.out.println("Value of b = "+b); int max = (a>b)? a:b; // here value of a will be stored in max as a>b is true in this example int min = (b>a)? a:b; // here value of b will be stored in min as b>a is false in this example System.out.println("Maximum of a and b = "+max); System.out.println("Minimum of a and b = "+min); } }

I hope you have understood all concepts behind Arithmetical Operators in Java. Thanks for reading the article.


Advertisements