When you enter into the basics of java operators play a role in the learning process of java .
The Coffee Shop Story
Imagine you are going out on a coffee date with your life partner. Java operators are just like the rules the shop uses to serve you.
Arithmetic Operators:
You order a coffee and your life partner orders 2. The cashier adds them:
1+2 = 3 coffees
That’s arithmetic—used for adding, subtracting, multiplying, or dividing.
Relational Operators
The waiters checks do you both have the same number of coffees?
If yes → true. If not → false.
These operators compare values.
Logical Operators
The manager says: “If BOTH of you pay (AND), you’ll get the order. If EITHER of you pays (OR), it still works.”
Logical operators help make decisions.
Assignment operators
The cashier writes your bill:
Bill = 200
Later, you add a sandwich:
Bill += 50 → 250
Assignment operators set or update values.
Ternary Operator
Finally, the waiter asks: “Are you a member? If yes → discount, else → full price.”
This is the ternary operator, a shortcut for quick decisions.
Just like the coffee shop runs smoothly with these rules, your Java program runs smoothly with operators—the symbols that calculate, compare, assign, and decide.
Now that you have got an basic understanding in java operators we will go through a real world example for a bit more clarification.
- A person buys 4 Packets of bread at ₹25 each and 2 milk packets at ₹30 each. Find the total bill.
public class GroceryBill {
public static void main(String[] args) {
int bread = 4 * 25;
int milk = 2 * 30;
int total = bread + milk;
System.out.println("Total GroceryBill = ₹" + total);
}
}
OUTPUT
Total Grocery Bill = ₹160
Step-by-Step Explanation
Multiplication (*)
int bread = 4 * 25;
Here, the person buys 4 loaves of bread.
Each bread costs ₹25.
Total bread cost = 4 * 25 = 100.
int milk = 2 * 30;
The person buys 2 packets of milk.
Each milk costs ₹30.
Total milk cost = 2 * 30 = 60.
Addition (+)
int total = bread + milk;
We add the bread cost (₹100) and milk cost (₹60).
Total = 100 + 60 = 160.
Output (System.out.println)
System.out.println("Total Grocery Bill = ₹" + total);
Total Grocery Bill = ₹160
What did we learn ?
(multiplication) helps calculate total cost when buying multiple items.
(addition) helps sum up the total bill.
Real-world shopping calculations can easily be represented in Java using arithmetic operators.
Top comments (0)