Advanced Java Concepts - Study Sheet
Outcome: Design robust, efficient, multithreaded, scalable Java applications
1. Collections Framework
Collections manage and store data efficiently. They allow adding, removing, and accessing data structures like lists,
sets, and maps.
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:
ArrayList<String> products = new ArrayList<>();
products.add("Laptop");
products.add("Phone");
System.out.println(products);
HashMap Example:
Map<Integer, String> productMap = new HashMap<>();
productMap.put(101, "Laptop");
productMap.put(102, "Phone");
System.out.println(productMap.get(101));
2. Multithreading
Multithreading enables concurrent execution, improving app performance when handling multiple tasks (like multiple
deliveries).
Thread Life-Cycle Example:
class DeliveryTask extends Thread {
public void run() {
System.out.println("Delivering order...");
}
}
DeliveryTask task = new DeliveryTask();
task.start();
Synchronization Example:
class Wallet {
private int balance = 1000;
synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
}
}
}
Inter-thread Communication Example:
Advanced Java Concepts - Study Sheet
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.");
}
User-Defined Exception Example:
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
class BankAccount {
private int balance = 500;
void withdraw(int amount) throws InsufficientFundsException {
if (amount > balance) {
throw new InsufficientFundsException("Insufficient funds");
}
balance -= amount;
}
}
Summary Table
| Concept | Real-World Example | Core Benefit |
|-------------------|--------------------------------|------------------|
| Collections | E-commerce catalog | Efficient data handling |
| Multithreading | Delivery app threads | Parallel processing |
| Synchronization | Banking transactions | Data consistency |
Advanced Java Concepts - Study Sheet
| Exception Handling | Banking error logs | Safe error recovery |
| User Exceptions | Age validation in signup forms | Clearer error messages |