Java Operators

Naveen AnamaneniUpdated · 5 min read

A calculator has keys for numbers and keys for actions: plus, minus, equals. In Java, the action keys are operators: symbols that take one or more values, called operands, and produce a new value. You've already used +, * and =. This lesson puts all the operator families side by side, so you know what each symbol does and what kind of result it gives back.

The Operator Families

Operator families and the kind of result each gives
FamilyOperatorsResultExample
Arithmetic+ - * / %A numberfare * 3
Relational== != < > <= >=booleanage >= 60
Logical&& || !booleanisPaid && hasSeat
Assignment= += -= *= /= %=The assigned valuetotal += 50
Unary++ -- + - !Depends on the operandcount++
Ternary? :One of two valuesage < 12 ? "child" : "adult"
Bitwise and shift& | ^ ~ << >> >>>A whole numberflags & 4

Three of these have their own lessons: "Ternary Operator", "Increment and Decrement Operators" and "Short-Circuit Evaluation".

Arithmetic and Remainder

Arithmetic.java▶ Try it
public class Arithmetic {
    public static void main(String[] args) {
        int sweets = 17;
        int children = 5;
        System.out.println("Sum:        " + (sweets + children));
        System.out.println("Product:    " + (sweets * children));
        System.out.println("Each gets:  " + (sweets / children));
        System.out.println("Left over:  " + (sweets % children));
    }
}
Output
Sum:        22
Product:    85
Each gets:  3
Left over:  2

How it works

  1. Line 7: / with two ints is whole-number division: 17 sweets shared by 5 children is 3 each.
  2. Line 8: % is the remainder (modulus) operator: 2 sweets are left over.
  3. The brackets matter: without them, "Sum: " + sweets + children would join the text "Sum: 17" and 5, giving Sum: 175.

Try it yourself: use % to check whether a number is even. What is 24 % 2, and what is 25 % 2?

Relational and Logical

Relational operators compare two values and give a boolean. Logical operators combine booleans:

Concession.java▶ Try it
public class Concession {
    public static void main(String[] args) {
        int age = 64;
        boolean hasId = true;
        boolean senior = age >= 60;
        boolean getsConcession = senior && hasId;
        System.out.println("Senior: " + senior);
        System.out.println("Concession: " + getsConcession);
        System.out.println("Not a senior: " + !senior);
    }
}
Output
Senior: true
Concession: true
Not a senior: false
OperatorMeansTrue when
a && bANDboth are true
a || bORat least one is true
!aNOTa is false

Assignment Shortcuts

total += 50; means total = total + 50;. The same works for -=, *=, /= and %=. These compound assignment operators also include a hidden cast back to the variable's type, as the lesson "Type Promotion in Expressions" showed.

Bitwise and Shift, Briefly

Bitwise operators work on the individual bits of whole numbers. You'll rarely need them in everyday business code, but they appear in flags, permissions, hashing and interviews:

ExpressionBinaryResult
6 & 3110 & 011 = 0102
6 | 3110 | 011 = 1117
6 ^ 3110 ^ 011 = 1015
5 << 1101 → 101010 (doubles)
20 >> 210100 → 1015 (divides by 4)

The lesson "Operator Precedence and Associativity" shows where they sit in the order of operations, and the algorithms lessons later in the course use them.

Common Mistakes

Dividing integers and expecting decimals. 7 / 2 is 3. Use 7 / 2.0 for 3.5.

Forgetting brackets when printing a sum. "Total: " + a + b joins text; write "Total: " + (a + b).

Using & and | instead of && and || for conditions. They compile, but they always evaluate both sides. The lesson "Short-Circuit Evaluation" shows why that matters.

Practice

  1. Easy: Given 1,000 rupees and items costing 75 rupees each, print how many items you can buy and how much money is left, using / and %.
  2. Harder: A movie ticket is free for children under 3 and for people with a free pass. Write the condition with age, hasPass and logical operators, and test it with three different people.

Quick check: Operators

4 questions
1What does 17 % 5 give?
2What type does age >= 60 produce?
3When is a || b true?
4What does "Total: " + 2 + 3 print?

Key Takeaways

Was this tutorial helpful?

Lesson 42 of 482