In this tutorial, we will learn operators in Java and their types with examples. Generally, we write a program to perform some basic operations.
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 operators are the building blocks of expressions. They allow us to perform arithmetic calculations, compare values, assign values, and control logic of program.
Java provides a rich set of operators that we use to perform different types of operations, such as arithmetic operations, relational comparisons, logical operations, bitwise operations, assignment operations, and more.
Java Operators
In Java, an expression has two main parts: operands and operators. The variables, constants, or values on which operators perform operations are called operands.
An operator in Java is a special symbol or a keyword that tells the compiler to perform a specific mathematical, relational, logical, or bitwise operation. Operators are used in programs to manipulate data and variables. They help form mathematical or logical expressions that produce a result.
Basic Definition of Operator in Java
In Java, an operator is a special symbol that performs a specific operation on one or more operands and produces a result.
What is an Expression in Java?
An expression is a combination of variables, constants, and operators that evaluates to a value. Consider an example.
int x = 10;
int result = x + 5;In the expression x + 5:
- x is an operand (variable).
- 5 is an operand (constant).
- + is an addition operator.
The addition operator + performs the addition operation on the operands x and 5 and produces the desired result.
Types of Operators in Java
In Java, we can classify operators in different ways based on the number of operands and their functionality.
1. Classification Based on Number of Operands
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. It uses a single operand. For example:
public class UnaryOperatorEx {
public static void main(String[] args) {
int x = 10;
x++; // Unary operator
System.out.println(x);
}
}Output:
11
In this example, x++; is an unary increment operator, which increases value by 1.
Examples of unary operators in Java include:
- + (unary plus)
- – (unary minus)
- ++ (increment)
- — (decrement)
- ! (logical NOT)
- ~ (bitwise NOT)
2. Binary Operator
The operator that acts on two operands is called binary operator. It uses two operands. For example:
public class BinaryOperatorExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2; // Binary operator
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
System.out.println("Sum: " + sum);
}
}Output:
First number: 10 Second number: 5 Sum: 15
Examples of binary operators in Java include:
- Arithmetic operators: +, -, *, /, %
- Relational operators: <, >, <=, >=, ==, !=
- Logical operators: &&, ||
- Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=
- Bitwise operators: &, |, ^
- Shift operators: <<, >>, >>>
3. Ternary Operator
The operator that acts on three operands is called ternary operator. It uses three operands. Java has only one ternary operator: the conditional operator (?:).
2. Classification Based on Symbol and Keyword
Based on the notation used, Java operators have been divided into two categories:
- Symbolic Operators
- Named Operators
A. Symbolic Operators
If a symbol like +, -, *, etc. is used as an operator, it is called symbolic operator. Examples of symbolic operators are:
- Arithmetic operators: +, -, *, /, %, ++, —
- Relational operators: <, >, <=, >=, ==, !=
- Logical operators: &&, ||, !
- Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=
- Unary operators: ++, —
- Conditional operators: ?:
- Bitwise operators: &, |, ^, ~
- Shift operators: <<, >>, >>>
B. Named Operator (Keyword Operator)
If a keyword is used as an operator, it is called named operator. Java has only one named operator:
- Instanceof operator
Let us understand first the arithmetic operators in Java with the help of various examples.
Arithmetic Operators in Java
Arithmetic operators in Java are those operators, which performs the basic arithmetic operations, such as addition, subtraction, multiplication, division, and remainder on numeric data types.
The numeric data types can be byte, short, int, long, float, and double. Java provides five main arithmetic operators, which are listed in the table below.
Table: Java Arithmetic Operators
| Operators | Meaning | Description |
|---|---|---|
| 1. + | Addition | Perform addition operation in which it adds two operands. |
| 2. – | Subtraction | Perform subtraction operation in which it subtracts second operand from first operand. |
| 3. * | Multiplication | Performs multiplication operation in which it multiplies two or more operands. |
| 4. / | Division | Performs division operation in which it divides first operand by second operand. |
| 5. % | Modulo division (Remainder) | Returns the remainder after division operation. |
Integer Arithmetic in Java
If both operands in an arithmetic expression are of integer types, such as byte, short, int, or long, the expression is called integer arithmetic expression, and the operation is called integer arithmetic. Integer arithmetic always takes produces an integer result.
Let’s take an example where we will perform addition, subtraction, multiplication, division, and modulus using integers.
Example 1:
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);
// Arithmetic operations.
System.out.println("a + b = " +(a + b)); // Addition
System.out.println("a - b = " +(a - b)); // Subtraction
System.out.println("a * b = " +(a * b)); // Multiplication
System.out.println("a / b = " +(a / b)); // Division
System.out.println("a % b = " +(a % b)); // Modulus
}
}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 us consider one more example based on integer arithmetic operations with negative values. This is very important program for beginners.
Example 2: Integer Arithmetic Operations with Negative Values
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)
This example program demonstrates:
- Integer subtraction (b – a)
- Multiplication with negative operand (-a * b)
- Integer division (a / b)
- Modulus with:
- negative dividend (-a % b)
- negative divisor (a % -b)
- both negative (-a % -b)
Explanation of Integer Division and Modulus in Java
- When both operands (a and b) are of integer types (int, byte, short, or long), the division operation performs integer division.
- In integer division, Java discards the decimal (fractional) part and keeps only the integer part. Therefore, the result of a/b is 1 because the decimal part has been truncated. However, the actual result is 1.666…, but Java removes the decimal part. This is called integer division.
- The modulus operator (%) returns the remainder after division.
- The sign of the modulus result is always the same as the sign of the dividend (first operand). It does not depend on the divisor (second operand).
- The result of -a % b is -20 because the dividend (-a) is negative, so the result is negative.
- The result of -a % -b is -20 because the dividend (-a) is negative, so the result is negative.
- Similarly, the result of a % -b is 20 because the dividend (a) is positive, so the result is positive.
Real Arithmetic in Java
An arithmetic operation that involves only real (floating-point) operands is called real arithmetic in Java. In real arithmetic, operands are of floating-point data types such as float and double. Floating-point values are represented in either decimal notation or exponential notation.
- Examples of decimal notation are 15.5, 20.2, etc.
- Examples of exponential notation are 1.55e1, 2.02e1, etc.
Let’s take an example where we will take values of variables in decimal form.
Example 3: Real Arithmetic Using Double
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
Note that Java uses IEEE 754 floating-point standard for float and double.
Mixed-mode Arithmetic in Java
When one operand is an integer type and the other operand are floating-point type, the expression is called mixed-mode arithmetic expression.
In mixed-mode arithmetic, Java automatically converts the integer operand into a floating-point operand before performing the operation. This process is called automatic type promotion or implicit type casting. After conversion, the operation is performed using floating-point arithmetic, and the result is also a floating-point value.
Let’s take an example where we will take one value as an integer and another value as floating-point.
Example 4: Mixed-Mode Arithmetic
public class MixedTest
{
int x = 20; // Integer operand
double y = 12.5; // Floating-point operand
void div()
{
double z = x / y; // Mixed-mode arithmetic
System.out.println("z = " +z);
}
public static void main(String[] args)
{
MixedTest mt = new MixedTest();
mt.div();
}
}Output:
z = 1.6
In this example:
- The variable x is of an integer (int) type and y is of a floating-point number (double) type.
- Since the second operand is double, Java automatically converts the integer operand x into a double.
- After conversion, the operation becomes double z = 20.0 / 12.5;
- Since the operation is now between two floating-point values, the result is also a floating-point value. Therefore, the output is 1.6.
Precedence of Arithmetic Operators in Java
The combination of variables, constants, and operators as per the syntax of the language is called arithmetic expression in Java. Java evaluates arithmetic expressions based on the precedence (priority) of operators.
If an expression does not contain parentheses, Java evaluates it according to the precedence rules and associativity of operators.
Operator Precedence Levels in Java
There are two precedence levels for an arithmetic operation in Java. They are:
1. Higher precedence: * / % (Evaluated first)
2. Lower precedence: + – (Evaluated after *, /, and %)
Role of Parentheses in Precedence
- If parentheses () are used in the expression, the arithmetic expression with parentheses has the highest priority.
- If two or more sets of parentheses are present into the expression one after another, Java evaluates the order from the left set towards the right set, starting with the innermost.
Let’ take an example program based on all the above concepts.
Example 5:
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)
Java does not have a built-in operator for exponentiation.
Conclusion
Operators in Java allow us to perform various operations on data and variables. We use operators to create expressions that manipulate values, perform calculations, compare data, and control program logic. Java provides different types of operators, such as arithmetic, relational, logical, assignment, bitwise, shift, unary, ternary, and instanceof.
In this tutorial, you learned about arithmetic operators in Java with various practical examples. Stay tuned with the next tutorial, where you will learn Unary operator in Java.





