Understand the Java Collection framework and the difference between Final, Finally, and Finalize keywords. These are essential concepts in Java programming for code organization and memory management.
The Collection Framework in Java provides a set of classes and interfaces to store and manipulate a group of objects efficiently. It is part of the java.util package.
• To represent a group of objects as a single unit.
• To provide ready-made data structures like List, Set, and Map.
• To improve code reusability and performance.
• Reduces development time with reusable data structures.
• Provides dynamic storage (no fixed size like arrays).
• Supports algorithms like sorting and searching easily.
• List Interface: ArrayList, LinkedList, Vector
• Set Interface: HashSet, LinkedHashSet, TreeSet
• Map Interface: HashMap, TreeMap, LinkedHashMap
import java.util.*;
public class Example {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list);
}
}
These three keywords look similar but serve completely different purposes in Java:
Used to declare constants, prevent inheritance, or stop method overriding.
final int MAX = 100;
Used in exception handling to execute important code whether exception occurs or not.
try {
int data = 50 / 0;
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("Finally block executed");
}
Used to perform cleanup operations before garbage collection.
protected void finalize() {
System.out.println("Finalize called before object deletion");
}
| Keyword | Usage | Belongs To |
|---|---|---|
| final | Constant / Prevent inheritance | Variable, Method, Class |
| finally | Used for cleanup code | Exception Handling |
| finalize | Called before object destruction | Object Class |
• Use Final keyword for constants and security.
• Always close resources in Finally block.
• Avoid relying on Finalize; use try-with-resources instead.