Java Operators
Learn how to perform calculations, comparisons, and logical operations on your data. Operators are the tools that make your program work.
What are Operators?
An operator is a symbol that tells Java to perform a specific action on one or more values. Operators are like the action buttons in your program. Just like a calculator uses + to add and - to subtract, Java uses operators to do math, make decisions, and assign values to variables.
- Arithmetic Operators: Used for mathematical calculations (addition, subtraction, etc.)
- Comparison Operators: Used to compare two values (equals, greater than, etc.)
- Logical Operators: Used to combine conditions (AND, OR, NOT)
- Assignment Operators: Used to assign values to variables
Think of it this way: If variables are like containers holding data, operators are like tools that work with that data. You pick the right operator for what you want to do, just like picking the right tool from a toolbox.
Arithmetic Operators
Arithmetic operators perform mathematical calculations. They work just like your calculator at school.
Example: Basic Arithmetic Operations
public class ArithmeticExample {
public static void main(String[] args) {
int a = 20;
int b = 8;
// Addition
int sum = a + b;
System.out.println("Addition: " + a + " + " + b + " = " + sum);
// Subtraction
int difference = a - b;
System.out.println("Subtraction: " + a + " - " + b + " = " + difference);
// Multiplication
int product = a * b;
System.out.println("Multiplication: " + a + " * " + b + " = " + product);
// Division
int quotient = a / b;
System.out.println("Division: " + a + " / " + b + " = " + quotient);
// Modulus (remainder)
int remainder = a % b;
System.out.println("Modulus: " + a + " % " + b + " = " + remainder);
// Increment
int x = 10;
x++;
System.out.println("After x++: " + x);
// Decrement
int y = 10;
y--;
System.out.println("After y--: " + y);
}
}
Addition: 20 + 8 = 28 Subtraction: 20 - 8 = 12 Multiplication: 20 * 8 = 160 Division: 20 / 8 = 2 Modulus: 20 % 8 = 4 After x++: 11 After y--: 9
Explanation:
- + Adds two numbers together (20 + 8 = 28)
- - Subtracts second number from first (20 - 8 = 12)
- * Multiplies two numbers (20 * 8 = 160)
- / Divides first number by second, gives whole number result (20 / 8 = 2)
- % Gives the remainder after division (20 % 8 = 4, because 20 ÷ 8 leaves remainder 4)
- ++ Increases value by 1 (x = 10 becomes 11)
- -- Decreases value by 1 (y = 10 becomes 9)
Assignment Operators
Assignment operators put values into variables. The most basic one is = which simply assigns a value. But you can also combine assignment with arithmetic for shortcuts.
Example: Different Assignment Operators
public class AssignmentExample {
public static void main(String[] args) {
int num = 10;
System.out.println("Initial value: " + num);
// Simple assignment
num = 15;
System.out.println("After num = 15: " + num);
// Add and assign (num = num + 5)
num += 5;
System.out.println("After num += 5: " + num);
// Subtract and assign
num -= 3;
System.out.println("After num -= 3: " + num);
// Multiply and assign
num *= 2;
System.out.println("After num *= 2: " + num);
// Divide and assign
num /= 4;
System.out.println("After num /= 4: " + num);
// Modulus and assign
num %= 5;
System.out.println("After num %= 5: " + num);
}
}
Initial value: 10 After num = 15: 15 After num += 5: 20 After num -= 3: 17 After num *= 2: 34 After num /= 4: 8 After num %= 5: 3
Explanation:
- = Simply assigns a value (num = 15)
- += Adds value and assigns it back (num += 5 means num = num + 5)
- -= Subtracts value and assigns it back (num -= 3 means num = num - 3)
- *= Multiplies value and assigns it back (num *= 2 means num = num * 2)
- /= Divides value and assigns it back (num /= 4 means num = num / 4)
- %= Gets remainder and assigns it back (num %= 5 means num = num % 5)
Comparison Operators
Comparison operators compare two values and always give you true or false as a result. They help your program make decisions.
Example: Comparing Values
public class ComparisonExample {
public static void main(String[] args) {
int x = 15;
int y = 10;
// Equal to
System.out.println("x == y: " + (x == y));
// Not equal to
System.out.println("x != y: " + (x != y));
// Greater than
System.out.println("x > y: " + (x > y));
// Less than
System.out.println("x < y: " + (x < y));
// Greater than or equal to
System.out.println("x >= 15: " + (x >= 15));
// Less than or equal to
System.out.println("y <= 10: " + (y <= 10));
// Comparing strings
String name1 = "Alice";
String name2 = "Alice";
System.out.println("name1 equals name2: " + name1.equals(name2));
}
}
x == y: false x != y: true x > y: true x < y: false x >= 15: true y <= 10: true name1 equals name2: true
Explanation:
- == Equal to: Checks if two values are exactly the same (15 == 10 is false)
- != Not equal to: Checks if two values are different (15 != 10 is true)
- > Greater than: Checks if first value is bigger (15 > 10 is true)
- < Less than: Checks if first value is smaller (15 < 10 is false)
- >= Greater than or equal to: Checks if first value is bigger or same (15 >= 15 is true)
- <= Less than or equal to: Checks if first value is smaller or same (10 <= 10 is true)
Logical Operators
Logical operators combine multiple conditions together. They help your program make complex decisions by checking if multiple things are true or false.
Example: Using Logical Operators
public class LogicalExample {
public static void main(String[] args) {
int age = 20;
int score = 85;
// AND (&&) - both conditions must be true
System.out.println("age > 18 && score > 80: " + (age > 18 && score > 80));
// AND - one condition is false
System.out.println("age > 18 && score > 90: " + (age > 18 && score > 90));
// OR (||) - at least one condition must be true
System.out.println("score > 90 || age > 18: " + (score > 90 || age > 18));
// OR - both conditions are false
System.out.println("score > 90 || age < 15: " + (score > 90 || age < 15));
// NOT (!) - reverses the result
boolean isStudent = true;
System.out.println("isStudent: " + isStudent);
System.out.println("!isStudent: " + !isStudent);
// Complex condition
boolean canVote = (age >= 18) && (score >= 50);
System.out.println("Can vote: " + canVote);
}
}
age > 18 && score > 80: true age > 18 && score > 90: false score > 90 || age > 18: true score > 90 || age < 15: false isStudent: true !isStudent: false Can vote: true
Explanation:
- && (AND): Both conditions must be true. If even one is false, result is false
- || (OR): At least one condition must be true. Only if both are false, result is false
- ! (NOT): Reverses/flips the condition. If something is true, ! makes it false and vice versa
Quick Reference: All Operators
Arithmetic Operators
+ Addition | - Subtraction | * Multiplication | / Division | % Modulus | ++ Increment | -- Decrement
Assignment Operators
= Assign | += Add and assign | -= Subtract and assign | *= Multiply and assign | /= Divide and assign | %= Modulus and assign
Comparison Operators
== Equal | != Not equal | > Greater than | < Less than | >= Greater or equal | <= Less or equal
Logical Operators
&& AND (both true) | || OR (at least one true) | ! NOT (reverses)
Operator Precedence (Order of Operations)
Just like in math, Java follows a specific order when using multiple operators. Multiplication and division happen before addition and subtraction. You can use parentheses () to control which operations happen first.
Example: Operator Order Matters
public class PrecedenceExample {
public static void main(String[] args) {
// Without parentheses: multiplication happens first
int result1 = 5 + 3 * 2;
System.out.println("5 + 3 * 2 = " + result1); // 3*2 = 6, then 5+6 = 11
// With parentheses: addition happens first
int result2 = (5 + 3) * 2;
System.out.println("(5 + 3) * 2 = " + result2); // 5+3 = 8, then 8*2 = 16
// Another example
int result3 = 10 - 4 + 2;
System.out.println("10 - 4 + 2 = " + result3); // Left to right: 10-4=6, then 6+2=8
// Using parentheses changes the result
int result4 = 10 - (4 + 2);
System.out.println("10 - (4 + 2) = " + result4); // 4+2=6, then 10-6=4
}
}
5 + 3 * 2 = 11 (5 + 3) * 2 = 16 10 - 4 + 2 = 8 10 - (4 + 2) = 4
Explanation:
- Operators have an order: *, / happen before +, -
- Operations of the same level go left to right
- Use parentheses () to control which operation happens first
- Parentheses always have the highest priority
Real-World Example: Checking Student Eligibility
Here's a practical example that combines multiple operators to make a real decision:
Example: Student Scholarship Eligibility
public class ScholarshipCheck {
public static void main(String[] args) {
// Student details
int marks = 85;
int attendance = 90;
int age = 20;
boolean isEligible = true;
// Check eligibility: marks > 80 AND attendance >= 85 AND age <= 22
boolean qualifies = (marks > 80) && (attendance >= 85) && (age <= 22);
System.out.println("Marks: " + marks);
System.out.println("Attendance: " + attendance);
System.out.println("Age: " + age);
System.out.println("Eligible for Scholarship: " + isEligible);
System.out.println("Qualifies for Award: " + qualifies);
// Another check
boolean needsSupport = (marks < 60) || (attendance < 75);
System.out.println("Needs Academic Support: " + needsSupport);
}
}
Marks: 85 Attendance: 90 Age: 20 Eligible for Scholarship: true Qualifies for Award: true Needs Academic Support: false
Explanation:
- We use comparison operators to check if marks > 80, attendance >= 85, and age <= 22
- We use logical AND (&&) to combine all three conditions - ALL must be true for scholarship
- We use logical OR (||) to check if a student needs support if marks are low OR attendance is low
- The program shows real decision-making: checking multiple conditions to determine eligibility