Java operators

Java operators perform arithmetic, compare values, test conditions, and assign results.

A simple example

Test two conditions

  1. Start with n is 2
  2. Do this Greater than 0 and less than 3?
  3. Result yes
Both comparisons succeed, so the program prints yes.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int n = 2;
        if (n > 0 && n < 3) {
            System.out.println("yes");
        }
    }
}
Output
yes
Shelf quantities: 2 and 5. Order needs: At least 5. Second shelf: 5 available.
Look up a shelf safely

A stock scanner receives a shelf position before it knows whether that shelf exists. Check the bounds first. Only an existing shelf with at least five units can supply the order.

Types of Operators

  1. Unary operator
  2. Arithmetic operator
  3. Relational operator
  4. Logical operator
  5. Bitwise operator
  6. Assignment operator
  7. Ternary operator