Stop 02 · Variables, Data Types & OperatorsTopics · 3/7

Operators & Expressions

Part of the Java Roadmap

In short

Calculating GST, checking if a user is old enough, deciding between two discounts: all of it comes down to operators. Most behave as you expect, but integer division and == on objects catch everyone at least once.

Java has arithmetic, relational, logical, assignment, increment/decrement and ternary operators. Integer division drops the decimal part, so 7 / 2 is 3.

The jaadu behind a small shop counter tallies groceries with an old-style calculator while a pan balance weighs a bag of rice.

Think of it like this

The kirana shop counter

The shopkeeper adds up items, multiplies by quantity, checks "is the total above ₹500 for free delivery?" and decides "cash or UPI?". Arithmetic, comparison, logic and a quick either-or choice: those are exactly Java's arithmetic, relational, logical and ternary operators.

Watch it happen

Evaluating 10 + 7 / 2 * 3 % 4

10
0
+
1
7 / 2
2
* 3
3
% 4
4

1/5Multiplication, division and remainder bind tighter than + and -, and run left to right.

The big ideas

What you need to know

Integer division

7 / 2 is 3 when both are ints. Make one side a double, like 7 / 2.0, to get 3.5. % gives the remainder.

Relational operators

==, !=, <, >, <=, >= return booleans. Use == for primitives; use equals() for Strings and objects.

Logical operators

&& (and), || (or), ! (not). && and || short-circuit: the right side is skipped if the left side decides the answer.

Shortcuts

total += price; count++; and the ternary: String fee = age < 12 ? "child" : "adult";

See it in code

Walk through the code

java
1public class Checkout {2    public static void main(String[] args) {3        int items = 3;4        double pricePerItem = 249.0;5        double subtotal = items * pricePerItem;6 7        boolean isMember = true;8        boolean freeDelivery = subtotal >= 500 || isMember;9        double delivery = freeDelivery ? 0 : 40;10 11        double total = subtotal + delivery;12        total -= isMember ? 50 : 0;13 14        System.out.println("Total: ₹" + total);15        System.out.println("7 / 2 = " + (7 / 2) + ", 7 % 2 = " + (7 % 2));16    }17}

1/5int times double gives a double: 747.0.

A checkout with arithmetic, a logical OR, the ternary operator and a compound assignment.

Save yourself the pain

Common mistakes

Oops

double avg = total / count; where both are int.

Do this instead

The division happens in ints first and the decimals are lost. Write (double) total / count.

Oops

if (x = 5) to compare.

Do this instead

= assigns. == compares. Java will not compile if (x = 5) for an int, which saves you here.

Oops

Expecting 0.1 + 0.2 == 0.3 to be true.

Do this instead

Floating-point values are approximations, so it is false. Compare with a small tolerance, or use BigDecimal for money.

Your mission

Canteen bill

~20 min

Quick check

What does this print: System.out.println(9 / 2 * 2);

Go deeper

  1. 1Compare 7 / 2, 7 / 2.0 and 7 % 2.
  2. 2Practise the difference between i++ and ++i in an expression.
  3. 3Use the ternary operator to pick the larger of two numbers.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge