Skip to content

Commit 37f4d04

Browse files
committed
feat: demonstrate type safety with Java Generics vs raw ArrayList usage
WHAT: - Added `Main.java` to show safe usage of `ArrayList<String>` with Generics. - Added `Main2.java` to illustrate potential runtime issues when using a raw `ArrayList` without Generics. HOW: 1. `Main.java`: - Created an `ArrayList<String>` to store only `String` values. - Type safety enforced at compile-time; retrieving elements does not require casting. - Example: `list.get(0)` directly returns a `String`. 2. `Main2.java`: - Created a raw `ArrayList` (no type parameter). - Mixed data types inserted: `String`, `Integer`, `Double`. - Explicit casting required when retrieving elements. - Demonstrates risk of `ClassCastException` at runtime (e.g., trying to cast an `Integer` to `String`). WHY: - Highlights the importance of **Generics** in Java collections. - Generics provide compile-time type safety and eliminate the need for manual casting. - Without Generics (raw types), type mismatches are only detected at runtime, leading to potential crashes. REAL-WORLD APPLICATIONS: - Generics are widely used in collections (`List<T>`, `Map<K,V>`, `Set<T>`) to ensure data consistency. - Useful in frameworks (Spring, Hibernate) to enforce strong typing. - Prevents runtime errors in large codebases by catching type mismatches during compilation. NOTES: - `Main2` will throw `ClassCastException` when attempting `String str1 = (String) list.get(1);` because the second element is actually an `Integer`. - Best practice: always use parameterized collections with Generics instead of raw types. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 819411e commit 37f4d04

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Generics;
2+
3+
import java.util.ArrayList;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
ArrayList<String> list = new ArrayList<>();
8+
list.add("Hello");
9+
list.add("World");
10+
String s = list.get(0);
11+
String s1 = list.get(1);
12+
13+
}
14+
}
15+
16+
/*
17+
The above code issues solved by Generics:
18+
19+
No Type safety
20+
Manual casting
21+
No Compile Time checking
22+
Generics can solve These issues.
23+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Generics;
2+
3+
import java.util.ArrayList;
4+
5+
public class Main2 {
6+
public static void main(String[] args) {
7+
ArrayList list = new ArrayList();
8+
list.add("Hello");
9+
list.add(123);
10+
list.add(3.14);
11+
12+
String str = (String) list.get(0);
13+
String str1 = (String) list.get(1);
14+
}
15+
}
16+
17+
/*
18+
Above code has 3 major issues:
19+
No Type safety
20+
Manual casting
21+
No Compile Time checking
22+
Generics can solve These issues. check code Main.
23+
*/

0 commit comments

Comments
 (0)