Basic Operators
An operator is a symbol that performs an operation on one or more operands. Java provides various kinds of operators: arithmetic for calculations, assignment for setting values, comparison for comparing two values, logical for boolean operations, and bitwise for bit manipulation.
One of Java's unique features is the instanceof operator, which checks whether an object is an instance of a particular class. Java 16+ supports pattern matching for instanceof, which performs the cast automatically. In addition, Java has the switch expression (since Java 14), a modern alternative to the traditional switch statement.
The ternary operator (? :) is a compact way to write a one-line if-else. The concatenation operator (+) on Strings is used very frequently in Java to combine text with variable values.
Types of Operators:
- Arithmetic:
+,-,*,/,%(modulus) - Assignment:
=,+=,-=,*=,/=,%= - Comparison:
==,!=,>,<,>=,<= - Logical:
&&(AND),||(OR),!(NOT) - Bitwise:
&,|,^,~,<<,>>,>>> - Ternary:
condition ? valueIfTrue : valueIfFalse - instanceof:
object instanceof Type
Code Example
public class BasicOperators {
public static void main(String[] args) {
// Arithmetic
int a = 17, b = 5;
System.out.println("a + b = " + (a + b)); // 22
System.out.println("a % b = " + (a % b)); // 2
// Comparison & logic
int x = 10, y = 20;
boolean result = (x > 5) && (y < 30);
System.out.println("Result: " + result); // true
// Ternary
int age = 20;
String status = (age >= 17) ? "Adult" : "Child";
System.out.println("Status: " + status); // Adult
// Compound assignment
int balance = 100000;
balance -= 25000; // balance = balance - 25000
System.out.println("Balance: " + balance); // 75000
}
}
Code Explanation
The arithmetic operators +, -, * work like a regular calculator. The % (modulus) operator returns the remainder of a division — very useful for checking whether a number is odd or even. Comparison operators produce a boolean value (true/false). && only produces true if both conditions are true. The ternary operator ? : is shorthand for if-else: if the condition is true, take the value after ?; if false, take the value after :. Compound assignment balance -= 25000 is short for balance = balance - 25000.
Everyday Example
A simple calculator program to compute a shopping total with discount and tax:
public class ShoppingCalculator {
public static void main(String[] args) {
int itemPrice = 75000;
int quantity = 3;
double discountPct = 10;
double taxPct = 11;
int subtotal = itemPrice * quantity;
double discount = subtotal * discountPct / 100;
int afterDiscount = (int) (subtotal - discount);
double tax = afterDiscount * taxPct / 100;
int finalTotal = (int) (afterDiscount + tax);
System.out.println("Subtotal: $" + subtotal);
System.out.println("Discount: -$" + (int) discount);
System.out.println("Tax: +$" + (int) tax);
System.out.println("Total: $" + finalTotal);
}
}
Summary
Operators are the foundation of every expression in Java. Understanding operator precedence is essential to avoid bugs. Use parentheses () explicitly to clarify the intended order of operations, especially when combining logical and arithmetic operators.