📚 Main Topics
Try-Catch-Finally Blocks
- Creating try blocks without catch or finally.
- Behavior of return statements in try and finally blocks.
- Nested try-catch blocks.
- When finally blocks may not execute.
Java Keywords
- Differences between
final, finally, and finalize.
Collections and Concurrency
- Handling exceptions in collections.
- Differences between
HashMap, ConcurrentHashMap, and SynchronizedHashMap. - Null keys and values in
HashMap and TreeMap.
Singleton Design Pattern
- Different ways to implement Singleton classes.
- Thread safety in Singleton implementations.
Java Inheritance and Interfaces
- Multiple inheritance in Java.
- Differences between abstract classes and interfaces.
- Functional interfaces and their properties.
Threading in Java
- Returning values from threads using
Callable. - Handling multiple exceptions in a single catch block.
Immutable Classes
- How to create immutable classes and their importance.
Sealed Classes
- Introduction to sealed classes and their benefits in inheritance control.
✨ Key Takeaways
- Try-Catch-FinallyYou can create a try block without a catch block if a finally block is present. In Java 8 and later, try-with-resources can be used without catch or finally.
- Return BehaviorIn a try-finally structure, the return statement in the finally block takes precedence over the try block.
- Null Handling
HashMap allows one null key and multiple null values, while TreeMap does not allow null keys unless handled in custom classes. - Singleton PatternsVarious methods exist to implement Singleton, including eager initialization, lazy initialization, and using enums for thread safety.
- Functional InterfacesCan extend other interfaces but must adhere to the rule of having only one abstract method.
- ThreadingUse
Callable for returning values from threads, as Runnable does not support return values. - Immutable ClassesMust be declared final, with private final fields and no setters to ensure immutability.
- Sealed ClassesIntroduced in Java 17, they restrict which classes can extend them, providing better control over inheritance.
🧠Lessons
- Understanding the nuances of Java's exception handling and concurrency can significantly impact your performance in interviews.
- Familiarity with the latest Java features (like sealed classes and records) is crucial for modern Java development.
- Knowing the differences between collections and their thread safety is essential for writing robust applications.
- Mastering the Singleton pattern and immutability principles can help in designing efficient and safe Java applications.
This summary encapsulates the key points discussed in the video, providing a comprehensive overview of tricky Java interview questions and their answers.