What Are Operators?
In Java, operators are symbols used to perform operations on variables
and values. They allow you tomanipulate data, perform calculations,
compare values, and make decisions.
For example, the + operator is used for addition.
Basic Arithmetic Example
int total = 75 + 25;
System.out.println("Total: " + total);
Output:
Total: 100
The + operator adds two numbers together.
Using Operators with Variables
Operators can work with:
- Two values
- A variable and a value
- Two variables
Example
int base = 40;
int bonus = 10;
int finalScore = base + bonus;
int doubleScore = finalScore * 2;
System.out.println("Final Score: " + finalScore);
System.out.println("Double Score: " + doubleScore);
Categories of Operators in Java
Java groups operators into several types based on their
functionality.
1. Arithmetic Operators
Used for mathematical calculations.
Table
Operator
+ -
*
Meaning
Addition
Subtraction
Multiplication
Division
Modulus (remainder)
/
%
Example
int a = 20;
int b = 6;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Remainder: " + (a % b));
2 . Assignment Operators
Used to assign values to variables.
Table
Operator
=
+=
Example
x = 5
x += 3
Meaning
Assign
Add and assign -=
*=
/=
x -= 2
x *= 4
x /= 2
Subtract and assign
Multiply and assign
Divide and assig
Example
int number = 10;
number += 5; // number = number + 5
System.out.println(number); // 15
3 .Comparison Operators
Used to compare two values.
They always return a boolean (true or false).
Operator
==
!=
Equal to
Meaning
Not equal to
>
Greater than
<
>=
<=
Example
int x = 15;
int y = 20;
Less than
Greater than or equal to
Less than or equal to
System.out.println(x > y); // false
System.out.println(x != y); // true
4 .Logical Operators
Used to combine multiple conditions.
Operator
Meaning
&&
!
Example
int age = 22;
Logical AND
Logical NOT
System.out.println(age > 18 && age < 30); //
true
System.out.println(!(age > 25));
// true
5 .Bitwise Operators
Used to perform operations on binary (bit-level) values.
Examples include:
- &
- |
- ^
- ~
- <<
- >
These are mainly used in low-level programming and advanced
scenarios.
What Are Arithmetic Operators?
Arithmetic operators in Java are used to perform basic mathematical
calculations such as addition, subtraction, multiplication, and
division. They work with numeric data types like int, float, and
double.
Common Arithmetic Operators
Table
Operator
+
Name
Addition
Purpose
Adds two values -
Subtraction
Subtracts one value from another
*
/
%
++ --
Multiplication
Division
Modulus
Increment
Decrement
Using Arithmetic Operators – Example
int a = 14;
int b = 4;
System.out.println(a + b); // 18
System.out.println(a - b); // 10
System.out.println(a * b); // 56
System.out.println(a / b); // 3
System.out.println(a % b); // 2
Multiplies values
Divides one value by another
Returns remainder
Increases value by 1
Decreases value by 1
Since both values are integers, division returns an integer
result.
Integer Division vs Decimal Division
When both operands are integers, Java performs integer
division.
Integer Division Example
int total = 25;
int parts = 4;
System.out.println(total / parts); // 6
Decimal Division Example
To get an accurate decimal result, use double values:
double total = 25.0;
double parts = 4.0;
System.out.println(total / parts); // 6.25
Increment (++) and Decrement (--) Operators
These operators are commonly used for counters, loops, and tracking
values.
Increment Example
int score = 7;
score++;
System.out.println(score); // 8
Decrement Example
int lives = 5;
lives--;
System.out.println(lives); // 4
Increment and Decrement Together
int number = 10;
number++;
number--;
System.out.println(number); // 10
Increasing and then decreasing returns the value to its original
state.
Real-Life Example: Tracking Visitors
Imagine counting people entering and leaving a library.
int visitors = 0;
// 4 people enter
visitors++;
visitors++;
visitors++;
visitors++;
System.out.println("Visitors inside: " + visitors); // 4
// 2 people leave
visitors--;
visitors--;
System.out.println("Visitors inside: " + visitors); // 2
What Are Assignment Operators?
Assignment operators are used to assign values to variables. The most
basic assignment operator is = which stores a value inside a
variable.
Basic Assignment Example
int quantity = 25;
System.out.println("Quantity: " + quantity);
Here, the value 25 is assigned to the variable quantity.
Compound Assignment Operators
Java also provides shortcut assignment operators that combine
arithmetic or bitwise operations with assignment.
Instead of writing:
value = value + 10;
You can write:
value += 10;
This makes the code shorter and easier to read.
Common Assignment Operators
Table
Operator
=
+= -=
Meaning
Assign
Add and assign
Subtract and assign
Equivalent To
x = 5
x = x + 5
x = x - 5
*=
/=
%=
&=
`
^=
>>=
<<=
Multiply and assign
Divide and assign
Modulus and assign
Bitwise AND assign
=`
Bitwise XOR assign
Right shift assign
Left shift assign
x = x * 5
x = x / 5
x = x % 5
x = x & 5
Bitwise OR assign
x = x ^ 5
x = x >> 5
x = x << 5
Example Using Multiple Assignment Operators
int points = 50;
points += 20; // 70
points -= 10; // 60
points *= 2; // 120
points /= 3; // 40
points %= 6; // 4
System.out.println("Final Points: " + points);
Bitwise Assignment Example
int number = 8; // 1000 in binary
number <<= 1; // Shift left
System.out.println("After left shift: " + number);
Bitwise assignment operators are mainly used in low-level or
performance-based programming.
Real-World Example: Managing Wallet Balance
Assignment operators are very useful in real-life applications like
tracking expenses.
int walletBalance = 500;
// Salary added
walletBalance += 2000;
// Grocery expense
walletBalance -= 750;
// Online purchase
walletBalance -= 300;
System.out.println("Current Wallet Balance: " +
walletBalance);
What Are Comparison Operators?
Comparison operators are used to compare two values or variables.
They help a program make decisions by checking conditions.
The result of every comparison is a boolean value:
- true
- false
These results are commonly used in if–else statements, loops, and
validations.
Simple Comparison Example
int a = 12;
int b = 7;
System.out.println(a > b); // true
Since 12 is greater than 7, the expression evaluates to
true.
List of Java Comparison Operators
Table
Operator
==
!=
Meaning
Equal to
Not equal to
Example
a == b
a != b
>
<
>=
<=
Greater than
Less than
Greater than or equal to
Less than or equal to
a > b
a < b
a >= b
a <= b
Example Using Multiple Comparisons
int marks = 65;
int passMark = 40;
System.out.println(marks >= passMark); // true
System.out.println(marks == 100);
// false
System.out.println(marks != 50); //
true
Real-World Example: Driving Eligibility
Check whether a person is allowed to apply for a driving
license.
int userAge = 17;
System.out.println(userAge >= 18); // false
System.out.println(userAge < 18); // true
The program confirms that the user is not yet eligible.
Real-World Example: Login Validation
Check if a username meets the minimum length
requirement.
int usernameLength = 6;
System.out.println(usernameLength >= 6); // true
System.out.println(usernameLength < 6); // false
What Are Logical Operators?
Logical operators are used to combine or modify boolean conditions
(true or false). They help a program make decisions based on multiple
conditions at the same time.
Logical operators are commonly used with:
- Comparison operators
- If–else statements
- Authentication and validation logic
Types of Logical Operators in Java
Table
Operator
&&
Name
Logical AND
Description
Returns true only if both
conditions are true
Example
a > 5 && a < 20
`
!
`
Logical NOT
Reverses the result of a
condition
Logical OR
!(a > 5)
Simple Logical AND Example (&&)
int number = 15;
System.out.println(number > 10 && number < 20); //
true
Both conditions are true, so the result is true.
Logical OR Example (||)
int temperature = 35;
System.out.println(temperature < 0 || temperature > 30); //
true
One condition is true (temperature > 30), so the result is
true.
Logical NOT Example (!)
boolean isRaining = true;
System.out.println(!isRaining); // false
The NOT operator reverses the boolean value.
Combining Multiple Logical Operators
int age = 22;
boolean hasID = true;
System.out.println(age >= 18 && hasID); //
true
The person is eligible because both conditions are
satisfied.
Real-Life Example: Website Access Control
Check whether a user can access a premium feature.
boolean loggedIn = true;
boolean premiumUser = false;
System.out.println("Can access premium: " + (loggedIn &&
premiumUser));
System.out.println("Can browse site: " + loggedIn);
System.out.println("Guest user: " + !loggedIn);
Output:
Can access premium: false
Can browse site: true
Guest user: false
Real-Life Example: Exam Eligibility
int attendancePercentage = 80;
boolean feesPaid = true;
System.out.println(attendancePercentage >= 75 &&
feesPaid); // true
Student is allowed to write the exam.
What Is Operator Precedence?
Operator precedence defines the order in which Java evaluates
operators in an expression that contains multiple
operators.
Java does not always calculate expressions from left to right.
Instead, it follows a fixed priority rule set—similar to
mathematics.
Basic Example of Operator Precedence
int valueA = 4 + 6 * 2; // 4 + 12 =
16
int valueB = (4 + 6) * 2; // 10 * 2 = 20
System.out.println(valueA);
System.out.println(valueB);
Output:
16
20
Explanation:
- In 4 + 6 * 2, multiplication happens first.
- Parentheses force Java to evaluate 4 + 6 before multiplying.
Why Parentheses Matter
Parentheses () always have the highest priority. They allow
you to control exactly how an expression is
evaluated.
- They prevent logical errors
- They improve code readability
- They make calculations predictable
Common Operator Precedence Order (High → Low)
Table
Priority
1
2
3
4
Operators
( ) Parentheses
* / %
+ -
< > <= >=
5
6
7
8
== !=
&& `
= Assignment
Example with Multiple Operators
int score = 20;
int bonus = 5;
int total = score + bonus * 2;
System.out.println(total); // 30
Explanation:
- bonus * 2 is calculated first → 10
- Then score + 10 → 30
Using Parentheses for Clarity
int score = 20;
int bonus = 5;
int total = (score + bonus) * 2;
System.out.println(total); // 50
Now addition happens first because of
parentheses.
Left-to-Right Evaluation Example
When operators have the same precedence, Java evaluates
from left to right.
int resultA = 30 / 5 * 2; // (30 / 5) * 2 =
12
int resultB = 30 / (5 * 2); // 30 / 10 = 3
System.out.println(resultA);
System.out.println(resultB);
Real-Life Example: Bill Calculation
int itemPrice = 200;
int quantity = 3;
int discount = 100;
int finalAmount = itemPrice * quantity -
discount;
System.out.println(finalAmount); // 500
With Parentheses:
int finalAmount = itemPrice * (quantity - 1);
System.out.println(finalAmount); // 400
Parentheses change the entire meaning of the
calculation.