Hello everyone, welcome back to the 100 Days of Java series.
Yesterday, we built our first calculator using Scanner. Pretty cool, right?
Today we are going to dive into something even more important: Operators and Expressions.
Without operators, your computer is basically useless. It is like Iron Man’s suit without power. Looks cool, but does nothing.
What Are Operators
Operators are symbols that tell the computer what action to perform.
Think of them as buttons on a calculator: +, -, *, and /.
In Java, there are several types of operators.
1. Arithmetic Operators
Used for basic math.
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder after division)
Example:
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1
2. Assignment Operators
Used to assign or update values.
= assigns a value
+= adds and assigns
-= subtracts and assigns
Example:
int x = 5;
x += 3; // same as x = x + 3, so x becomes 8
3. Relational Operators (Comparison)
Used to compare two values. They return true or false.
== equal
!= not equal
> greater
< less
>= greater or equal
<= less or equal
Example:
System.out.println(10 > 5); // true
System.out.println(10 == 5); // false
4. Logical Operators
Used for true or false decisions.
&& AND (both must be true)
|| OR (either can be true)
! NOT (flips true or false)
Example:
System.out.println((5 > 3) && (8 > 6)); // true
System.out.println((5 > 10) || (8 > 6)); // true
System.out.println(!(5 > 3)); // false
5. Increment and Decrement Operators
Increase or decrease a value by one.
++ increase by 1
-- decrease by 1
Example:
int y = 5;
y++; // y becomes 6
y--; // y becomes 5
Grocery Price Calculator
Now let’s use these operators in a small project.
We will build a grocery price calculator that asks for item quantities and calculates the total bill.
import java.util.Scanner;
public class GroceryCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prices of items
double ricePrice = 50.0; // per kg
double milkPrice = 25.0; // per liter
double eggPrice = 5.0; // per egg
System.out.println("Welcome to Grocery Calculator!");
System.out.print("Enter kg of rice: ");
double riceKg = sc.nextDouble();
System.out.print("Enter liters of milk: ");
double milkLiters = sc.nextDouble();
System.out.print("Enter number of eggs: ");
int eggs = sc.nextInt();
// Total calculations
double totalRice = riceKg * ricePrice;
double totalMilk = milkLiters * milkPrice;
double totalEggs = eggs * eggPrice;
double total = totalRice + totalMilk + totalEggs;
// Discount example using operators
double discount = 0;
if (total > 500) {
discount = total * 0.1; // 10 percent discount
}
double finalAmount = total - discount;
// Output
System.out.println("------ Bill ------");
System.out.println("Rice: " + totalRice);
System.out.println("Milk: " + totalMilk);
System.out.println("Eggs: " + totalEggs);
System.out.println("Total: " + total);
System.out.println("Discount: " + discount);
System.out.println("Final Amount: " + finalAmount);
}
}
Example Run
Welcome to Grocery Calculator!
Enter kg of rice: 5
Enter liters of milk: 2
Enter number of eggs: 12
------ Bill ------
Rice: 250.0
Milk: 50.0
Eggs: 60.0
Total: 360.0
Discount: 0.0
Final Amount: 360.0
If you buy more than 500 worth, you get a 10 percent discount.
Iron Man Reference
Think of operators like the gears inside Iron Man’s helmet.
Tony says, “Target locked.”
Behind the scenes, operators are doing the math and logic.
Is the enemy in range? (comparison operator)
Should I fire? (logical operator)
Reduce ammo count (assignment and decrement operator)
Without operators, Jarvis is just a glowing screen saver.
Wrap Up
Today we learned:
- What operators are and their main types
- Arithmetic, assignment, relational, logical, and increment or decrement operators
- Built a grocery price calculator with discount logic
Tomorrow, we will take these operators and use them in control statements, teaching our program how to make decisions.
References
You can also check out:
- Learn Java Basics (Day 1) – Install JDK and Run Your First Program
- Learn Java Variables and Data Types (Day 2): Java for Beginners
- Day 3: Java Variables and Data Types Explained with Real-Life Examples
- Day 4 Java Tutorial: Scanner Input + Build a Basic Calculator
- Beginner’s Guide to Web Development
About Us
- Akhil Boddu’s YouTube Channel – Follow my vlogs for personal experiences and tips.
- Code With Me YouTube Channel – Explore tech tutorials and coding advice.
- WeblogTrips LinkedIn – Connect with me on LinkedIn for more insights and discussions.
If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.







