Wrapper Classes & Generics ๐¶
Mentor's Note: Primitives like
intare fast, but they aren't Objects. To use them in powerful tools like ArrayLists, we need to "Wrap" them. Generics then ensure we don't accidentally put a "Spoon" into a "Shoe Box"! ๐ก
๐ The Scenario: The Cardboard Box ๐ฆ¶
Imagine you have a single piece of fruit ๐.
- The Primitive: The fruit is raw and loose. It's fast to eat but hard to ship or organize in a large warehouse. ๐ฆ
- The Wrapper: You put the fruit in a Cardboard Box ๐ฆ. Now it's an "Object." It has labels and can be handled by standard shipping tools. (Autoboxing).
- The Generics: You label the warehouse section: "APPLES ONLY" ๐ท๏ธ. Now, workers cannot accidentally put an Onion in your Apple box.
- The Result: You have organized, safe, and professional storage. โ
๐ Concept Explanation¶
1. Wrapper Classes¶
Wrapper classes allow us to use primitive types (int, char, double) as objects.
| Primitive | Wrapper |
|---|---|
byte |
Byte |
int |
Integer |
char |
Character |
boolean |
Boolean |
- Autoboxing: Automatic conversion:
Integer myObj = 5;๐ช - Unboxing: Reverting back:
int i = myObj;๐ช
2. Generics¶
Generics allow you to create classes, interfaces, and methods that work with Any Type, while checking for errors at Compile Time. ๐ก๏ธ
๐จ Visual Logic: Type Safety¶
graph LR
A[Raw List] -- "add 'Hi'" --> B[Accepts Anything โ]
A -- "add 10" --> B
C["Generic List <String>"] -- "add 'Hi'" --> D[Success โ
]
C -- "add 10" --> E[Compile Error! ๐]
style E fill:#f99
๐ป Implementation: The Advanced Lab¶
// ๐ Scenario: A Universal Storage Box
// ๐ Action: Using T as a placeholder for any type
class Box<T> {
private T content;
public void set(T item) { this.content = item; }
public T get() { return this.content; }
}
public class Main {
public static void main(String[] args) {
// ๐ฆ 1. Create a String Box
Box<String> nameBox = new Box<>();
nameBox.set("VishnuDigital");
// ๐ฆ 2. Create an Integer Box
Box<Integer> scoreBox = new Box<>();
scoreBox.set(100);
System.out.println("Stored: " + nameBox.get());
}
}
๐ Sample Dry Run (Autoboxing)¶
| Instruction | What Java Sees | Description |
|---|---|---|
Integer n = 10; |
Integer.valueOf(10) |
Primitive "wrapped" into Object ๐ฆ |
int x = n; |
n.intValue() |
Object "unwrapped" into primitive ๐ |
๐ Technical Analysis¶
- Type Erasure: In Java, Generics are only for the compiler. Once the code is compiled, Java removes the type info (
<T>) to stay backward compatible. ๐ง - Constraints: You cannot use primitives directly in Generics.
ArrayList<int>is INVALID; useArrayList<Integer>.
๐ฏ Practice Lab ๐งช¶
Task: The Generic Swapper
Task: Write a generic method printValue(T value) that prints whatever value is passed to it, along with its data type.
Hint: value.getClass().getName(). ๐ก
๐ก Interview Tip ๐¶
"Interviewers love asking: 'What is the benefit of Generics?' Answer: It provides Type Safety at compile time and eliminates the need for manual Type Casting!"
๐ก Pro Tip: "Before software can be reusable it first has to be usable." - Ralph Johnson