Operators in Java | Arithmetic Operators, Example

A programmer generally makes a program to perform some operation. For example, we can perform the addition of two numbers just by using + symbol in a program.

Here, + is a symbol that performs an operation called addition. Such a kind of symbol is called operator in Java.

Understanding operators is a basic in Java or any other programming languages because they are the building blocks of expressions and enable us to manipulate data in various ways.

In this tutorial, we will discuss different types of operators in Java, and their functions, usages with the help of examples.

Java Operators


In Java, an expression has two parts: operand and operator. The variables or constants that operators act upon are called operands.

An operator in Java is a symbol or a keyword that tells the compiler to perform a specific mathematical or logical operations.

They are used in programs to manipulate data and variables. They generally form mathematical or logical expressions. An expression is a combination of variables, constants, and operators.

For example, an expression is x + 5. Here, the operand x is a variable, operand 5 is a constant, and + is an operator that acts on these two operands and produces the desired result.

Operators in Java

Types of Operators in Java


There are three types of operators in Java based on the number of operands used to perform an operation. These operators are shown in the below figure.

Types of operators in Java

1. Unary operator: The operator that acts on a single operand is called unary operator. A unary operator uses a single variable.

2. Binary operator: The operator that acts on two operands is called binary operator. A binary operator uses two variables.

3. Ternary operator: The operator that acts on three operands is called ternary operator. A ternary operator uses three variables.

Based on the notation used, Java operators have been divided into two categories: Symbolic and named.


If a symbol like +, -, *, etc is used as an operator, it is called symbolic operator. If a keyword is used as an operator, it is called named operator.

Classification of Operators based on Symbols and Named in Java


The Java operators can be classified on the basis of symbols and named. They are as follows:

1. Symbolic operator in Java

  • Arithmetic operators:       +, -, *, /, etc.
  • Relational operators:       <, >, <=, >=, = =, !=.
  • Logical operators:            &&, ||, !.
  • Assignment operators:     =,
  • Increment and decrement operators: + +, – –
  • Conditional operators:     ?:
  • Bitwise operators:           &, !, ^, ~, <<, >>, >>>
  • Shift operators:               <<, >>, >>>.

2. Named operators in Java

  • Instanceof operator

Let us understand the various types of operators in detail.

Arithmetic Operators in Java


Java Arithmetic operators are used to performing fundamental arithmetic operations such as addition, subtraction, multiplication, and division on numeric data types.

The numeric data types can be byte, short, int, long, float, and double. Java provides five arithmetic operators. They are listed in the below table.

Table: Java Arithmetic Operators

OperatorsMeaningDescription
1. +Addition or unary plusPerforms addition operation.
2. –Subtraction or unary minusPerforms subtraction operation.
3. *MultiplicationPerforms multiplication operation.
4. /DivisionPerforms division operation.
5. %Modulo division (Remainder)Performs remainder after division operation.

Integer Arithmetic

If both operands in a single arithmetic expression are integers, the expression is called arithmetic expression, and the operation is called integer arithmetic. Integer arithmetic always takes integer values.


Let’s take an example where we will implement all the above arithmetic operators shown in the table.

Program code 1:

package arithmeticPrograms; 
public class IntegerTest { 
public static void main(String[] args) 
{ 
// Declaration of instance variables with initialization. 
   int a = 20, b = 10; 
 
   System.out.println("a: " +a); 
   System.out.println("b: " +b); 
   System.out.println("a + b = " +(a + b)); 
   System.out.println("a - b = " +(a - b)); 
 
   System.out.println("a * b = " +(a * b)); 
   System.out.println("a / b = " +(a / b)); 
   System.out.println("a % b = " +(a % b)); 
 } 
}
Output: 
            a: 20 
            b: 10 
            a + b = 30 
            a - b = 10 
            a * b = 200 
            a / b = 2 
            a % b = 0 (Remainder of integer division)

Let’s take one more example based on integer arithmetic operators.

Program code 2:

package arithmeticPrograms; 
public class IntegerTest2 
{ 
// Declaration of instance variables. 
   int a = 50, b = 30; 

// Declaration of instance methods. 
   void m1()
   { 
     int p = b - a; 
     System.out.println("p = " +p); 
   } 
   void m2()
   { 
     int q = -a * b; 
     System.out.println("q = " +q); 
   } 
   void m3()
   {  
     int r = a / b; 
     System.out.println("r = " +r); 
   } 
   void m4()
   { 
     int s = -a % b; 
     int t = -a % -b; 
     int u = a % -b;
   
     System.out.println("s = " +s); 
     System.out.println("t = " +t); 
     System.out.println("u = " +u); 
    } 
public static void main(String[] args) 
{ 
// Creating an object of class. 
   IntegerTest2 i = new IntegerTest2(); 
   
    i.m1(); // Calling of m1() method. 
    i.m2(); // Calling of m2() method. 
    i.m3(); // Calling of m3() method. 
    i.m4(); // Calling of m4() method. 
  } 
}
Output: 
       p = -20 
       q = -1500 
       r = 1 
       s = -20 
       t = -20 (Decimal part truncated) 
       u = 20 (Remainder of integer division)

1. In the preceding example, a and b are integer types. Therefore, the result of a/b is 1 because the decimal part (divisor) has been truncated. This operation is called integer division.

2. In the case of modulus division, the sign of result is always sign of the dividend (i.e, first operand).
For example :

  • -a % b = -20 because the sign of a (dividend) is minus.
  • -a % -b = -20 because the sign of a is minus.
  • a % -b = 20 because the sign of a is plus.

Real Arithmetic

An arithmetic operation that involves only real operands is called real arithmetic. In real operand, values are assumed either in decimal or exponential notation.

Let’s take an example program where we will assume values of variables in decimal form.

Program code 3:

package arithmeticPrograms; 
public class RealTest 
{ 
public static void main(String[] args) 
{ 
  double a = 15.5, b = 20.2; 
  System.out.println("a + b = " +(a + b)); 
  System.out.println("a - b = " +(a - b)); 
 
  System.out.println("a * b = " +(a * b)); 
  System.out.println("a / b = " +(a / b)); 
  System.out.println("a % b = " +(a % b)); 
 } 
}
Output: 
       a + b = 35.7 
       a - b = -4.699999999999999 
       a * b = 313.09999999999997 
       a / b = 0.7673267326732673 
       a % b = 15.5

Mixed-mode Arithmetic

When one operand is integer and the other operands are real, this kind of expression is called mixed-mode arithmetic operation. First, integer operand is converted into real operand and then real arithmetic operation is performed.

Let’s take an example where we will take one value as an integer and another value as real.

Program code 4:

package arithmeticPrograms; 
public class MixedTest 
{ 
 int x = 20; // Integer 
 double y = 12.5; // Real 

 void div()
 { 
   double z = x/y; 
   System.out.println("z = " +z); 
 } 
public static void main(String[] args) 
{ 
  MixedTest mt = new MixedTest(); 
  mt.div(); 
 } 
}
Output: 
             z = 1.6

In the above example program, you can see that the result is in decimal point because integer operand has been first converted into real operand.

Precedence of Arithmetic Operators


The combination of variables, constants, and operators as per the syntax of the language is called arithmetic expression in java.

It is evaluated from left to right using the rules of precedence of operators if an arithmetic expression has no parentheses.

There are two priority levels of an arithmetic operation in Java. They are as follows:

1. High priority: * /  %
2. Low priority: + –

If parentheses are used in the expression, the expression with parentheses will be assumed with the highest priority.

If two or more sets of parentheses occur into the expression one after another, the order of evaluation will be done from the left set towards the right set.

Let’ take an example program based on all the above concepts.

Program code 5:

package arithmeticPrograms; 
public class MyTest 
{ 
 int x = 9; 
 int y = 12; 
 int z = 3; 
void m1()
{ 
 int exp1 = x - y / 3 + z * 2 - 1; 
 System.out.println("Evaluation1 = " +exp1); 
} 
void m2()
{ 
 int exp2 = (x - y)/3 + ((z * 2) - 1); 
 System.out.println("Evaluation2 = " +exp2); 
} 
public static void main(String[] args) 
{ 
  MyTest t = new MyTest(); 
   t.m1(); // Calling of m1 method. 
   t.m2(); // Calling of m2 method. 
 } 
}
Output: 
           Evaluation1 = 10 
           Evaluation2 = 4

Explanation of exp1: exp1 has been evaluated by the following steps. They are as follows:

exp1 = 9-12/3+3*2-1

Step 1: exp1 = 9-4+3*2-1 (12/3 evaluated)
Step 2: exp1 = 9-4+6-1 (3*2 evaluated)
Step 3: exp1 = 5+6-1 (9-3 evaluated)
Step 4: exp1 = 11-1 (6+5 evaluated)
Step 5: exp1 = 10 (11-1 evaluated)

Explanation of exp2: exp2 has been evaluated by the following steps. They are as follows:

exp2 = (9-12)/3+((3*2)-1)

Step 1: exp2 = -3/3+((3*2)-1) (9-12 evaluated)
Step 2: exp2 = -3/3+(6-1) (3*2 evaluated)
Step 3: exp2 = -3/3+5 (6-1 evaluated)
Step 4: exp2 = -1+5 (-3/3 evaluated)
Step 5; exp2 = 4 (-1+5 evaluated)


Key points: Java does not have an operator for exponentiation.


Recommended Tutorials on Java Operators:


Hope that this tutorial has covered all important basic points related to operators in Java with example programs. I hope that you will have understood arithmetic operators and practiced all example programs. If the tutorial is useful, please share it on social networking with your friends. In the next, we will understand relational operators with the help of examples.
Thanks for reading!!!

⇐ Prev Next ⇒

Please share your love