Skip to content

Commit ffe0383

Browse files
committed
Section 24 Java Generics
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent af3ad78 commit ffe0383

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Section 24 – Java Generics (Quick Notes)
2+
3+
## What Are Generics?
4+
- Generics allow you to write classes, interfaces, and methods with type parameters.
5+
- They provide type safety and eliminate the need for explicit casting.
6+
- Example: List<String> ensures only Strings are stored.
7+
8+
## Benefits
9+
- Type Safety → Catch errors at compile time instead of runtime.
10+
- Code Reusability → Write flexible code that works with different data types.
11+
- Cleaner Code → No need for casting, better readability.
12+
13+
## Syntax
14+
- Generic Class: class Box<T> { T value; }
15+
- Generic Method: <T> void print(T data)
16+
- Bounded Types: <T extends Number> restricts T to subclasses of Number
17+
- Wildcards:
18+
- <?> → unknown type
19+
- <? extends T> → upper bounded wildcard
20+
- <? super T> → lower bounded wildcard
21+
22+
## Real-World Usage
23+
- Collections Framework (List<T>, Set<T>, Map<K, V>)
24+
- Generic algorithms like sort, search
25+
- Dependency injection and frameworks (Spring, Hibernate)
26+
27+
## Why Important?
28+
- Generics make APIs flexible and type-safe.
29+
- Widely used in modern Java applications for collections, utility classes, and framework design.

0 commit comments

Comments
 (0)