Skip to content

Commit d22328f

Browse files
committed
feat(generic-methods): add generic printArray method for arrays of any type
WHAT: - Implemented `printArray(T[] array)` as a static generic method. - Demonstrated usage with both `Integer[]` and `String[]`. WHY: - To showcase how a single generic method can handle arrays of different types without duplicating code for each type. - Emphasizes type safety and reusability in generic programming. HOW: - Declared method with type parameter `<T>`. - Iterated over the array using an enhanced for-loop to print each element. - Added calls in `main` to test with integer and string arrays. BENEFITS: - Eliminates redundancy compared to writing multiple overloaded methods. - Increases flexibility — works with any object type. - Provides a clear example of **generic methods in action**. REAL-WORLD APPLICATIONS: - Utility methods in frameworks/libraries (e.g., array to string/logging/validation). - Generic data exporters or serializers that must handle different object arrays uniformly. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 29e28a5 commit d22328f

File tree

1 file changed

+70
-0
lines changed
  • Section24JavaGenerics/src/GenericMethods

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package GenericMethods;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
/*
6+
Test test = new Test();
7+
Integer[] intArray = {1,2,3,4};
8+
String[] stringsArray = {"Hello","World"};
9+
test.printArray(intArray);
10+
test.printArray(stringsArray);
11+
*/
12+
13+
Integer[] intArray = {1,2,3,4};
14+
String[] stringsArray = {"Hello","World"};
15+
printArray(intArray);
16+
printArray(stringsArray);
17+
}
18+
public static <T> void printArray(T[] array) {
19+
//method body.
20+
for (T element : array) {
21+
System.out.println(element + " ");
22+
}
23+
System.out.println();
24+
}
25+
}
26+
27+
/*
28+
1. Generic Method Definition:
29+
- `public static <T> void printArray(T[] array)`
30+
- Yaha `<T>` ek **type parameter** hai jo batata hai ki method generic hai.
31+
- Matlab tum kisi bhi type ka array pass kar sakte ho (Integer[], String[], Double[], etc.).
32+
33+
2. Usage:
34+
- `printArray(intArray);` → Integer[] ke liye kaam karega.
35+
- `printArray(stringsArray);` → String[] ke liye kaam karega.
36+
- Compiler automatically T = Integer ya T = String decide karega (type inference).
37+
38+
3. for-each Loop:
39+
- `for (T element : array)`
40+
- Har element ko loop me print karta hai, bina is baat ki tension liye ki array kis type ka hai.
41+
42+
4. Output Example:
43+
- Agar `intArray = {1,2,3,4}` hai → output hoga:
44+
```
45+
1
46+
2
47+
3
48+
4
49+
```
50+
- Agar `stringsArray = {"Hello","World"}` hai → output hoga:
51+
```
52+
Hello
53+
World
54+
```
55+
56+
5. Why Generic Method instead of Overloading?
57+
- Agar tumne alag methods banaye hote:
58+
```java
59+
void printArray(Integer[] arr) {...}
60+
void printArray(String[] arr) {...}
61+
```
62+
toh har type ke liye duplicate code likhna padta.
63+
- Generic method ek hi jagah reusable code deta hai, type-safe bhi hai.
64+
65+
66+
✔ `<T>` → method ko generic banata hai.
67+
✔ Compiler call ke time pe type decide karta hai.
68+
✔ Same method Integer[], String[], Double[] arrays ke liye reuse hota hai.
69+
✔ Clean, reusable aur type-safe solution bina overloading ke.
70+
*/

0 commit comments

Comments
 (0)