Operators & Expressions
Operators are special symbols that perform operations on variables and values. Java has arithmetic, comparison, logical, and assignment operators. Let's master them all with practical examples. This is a foundational concept in enterprise application development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world Java experience. Take your time with each section and practice the examples
Arithmetic Operators
Arithmetic operators perform mathematical calculations. These are the most commonly used operators in programming — you'll use them in almost every program you write.. This is an essential concept that every Java developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Arithmetic Examples
public class ArithmeticOps {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println("a + b = " + (a + b)); // Addition: 13
System.out.println("a - b = " + (a - b)); // Subtraction: 7
System.out.println("a * b = " + (a * b)); // Multiplication: 30
System.out.println("a / b = " + (a / b)); // Division: 3 (integer)
System.out.println("a % b = " + (a % b)); // Modulus (remainder): 1
// Increment and Decrement
int count = 5;
count++; // count is now 6
System.out.println("After count++: " + count);
count--; // count is now 5 again
System.out.println("After count--: " + count);
}
}Try It Yourself: Calculator
Comparison & Logical Operators
public class ComparisonOps {
public static void main(String[] args) {
int x = 10;
int y = 20;
// Comparison operators (return true/false)
System.out.println("x == y: " + (x == y)); // Equal to: false
System.out.println("x != y: " + (x != y)); // Not equal: true
System.out.println("x > y: " + (x > y)); // Greater than: false
System.out.println("x < y: " + (x < y)); // Less than: true
System.out.println("x >= 10: " + (x >= 10)); // Greater or equal: true
System.out.println("x <= 5: " + (x <= 5)); // Less or equal: false
// Logical operators
boolean isSunny = true;
boolean isWarm = false;
System.out.println("AND: " + (isSunny && isWarm)); // false
System.out.println("OR: " + (isSunny || isWarm)); // true
System.out.println("NOT: " + (!isSunny)); // false
}
}