Advanced Java Concepts Study Sheet
Advanced Java Concepts Study Sheet
Real World Example: E-commerce product catalog uses ArrayList for ordered product lists, Set for unique categories,
and Map for productId to product mapping.
ArrayList Example:
HashMap Example:
2. Multithreading
Multithreading enables concurrent execution, improving app performance when handling multiple tasks (like multiple
deliveries).
Synchronization Example:
class Wallet {
private int balance = 1000;
synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
}
}
}
class Kitchen {
boolean orderReady = false;
synchronized void prepareOrder() throws InterruptedException {
orderReady = true;
notify();
}
synchronized void waitForOrder() throws InterruptedException {
while (!orderReady) wait();
System.out.println("Order is ready!");
}
}
3. Exception Handling
Exception handling ensures applications gracefully handle runtime errors.
Try-Catch Example:
try {
int balance = 1000 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Transaction complete.");
}
class BankAccount {
private int balance = 500;
void withdraw(int amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds");
}
balance -= amount;
}
}
Summary Table