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

**operators in java**.

So, let us understand different types of operators and expressions supported by java in this tutorial and learn how to use them with examples.

**Topics Covered in Tutorial**hide

## 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.

## 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.

### 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 operator has 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

**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 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 char, byte, short, int, long, float, and double. Java provides five arithmetic operators. They are listed in the below table.

### Table: Java Arithmetic Operators

Operators | Meaning | Description |
---|---|---|

1. + | Addition or unary plus | Performs addition operation. |

2. – | Subtraction or unary minus | Performs subtraction operation. |

3. * | Multiplication | Performs multiplication operation. |

4. / | Division | Performs 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 create a program where we will implement all the above arithmetic operators shown in the table.**Program source 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 program related to integer arithmetic operators.**Program source 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. i.m3(); // Calling of m3. i.m4(); // Calling of m4. } }

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 program, 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 sign of a is plus.

### Real Arithmetic

**Program source 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**

**Program source 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

1. High priority: * / %

2. Low priority: + –

If parentheses are used into 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 left set towards the right set.

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

**Program source 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 posts on Operators in Java:**

- Relational Operators in Java
- Logical Operators in Java
- Assignment Operators in Java
- Unary Operators in Java
- Conditional (Ternary) Operator in Java
- Bitwise Operators in Java
- Shift Operators in Java

**Final words**

Hope that this tutorial has covered almost all important points related to **operators in java** with example programs. I hope that you will have understood **arithmetic operators in java** and enjoyed it.

If the tutorial is useful, please share it on social networking for your friends.

Thanks for reading!!!** Next ⇒ Relational Operators in Java**⇐ PrevNext ⇒