File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Section24JavaGenerics/src/Generics Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments