-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsExample.java
More file actions
102 lines (86 loc) · 2.54 KB
/
Copy pathGenericsExample.java
File metadata and controls
102 lines (86 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package java5.generics;
import java.util.*;
/**
* Java 5 Generics Example Demonstrates type-safe generic programming
*/
public class GenericsExample
{
public static void main(String[] args)
{
System.out.println("=== Java 5 Generics ===\n");
System.out.println("1. Generic Class");
System.out.println("---------------");
Box<String> stringBox = new Box<>();
stringBox.setItem("Hello, Generics!");
System.out.println("String box: " + stringBox.getItem());
Box<Integer> intBox = new Box<>();
intBox.setItem(42);
System.out.println("Integer box: " + intBox.getItem());
System.out.println("\n2. Generic Methods");
System.out.println("-----------------");
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Java", "Python", "C++"};
printArray(intArray);
printArray(strArray);
System.out.println("\n3. Generic Collections");
System.out.println("--------------------");
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println("List: " + list);
String first = list.get(0); // No casting needed
System.out.println("First element: " + first);
Map<String, Integer> map = new HashMap<>();
map.put("Java", 1995);
map.put("Python", 1991);
System.out.println("Map: " + map);
Integer year = map.get("Java"); // No casting needed
System.out.println("Java year: " + year);
System.out.println("\n4. Wildcards");
System.out.println("-----------");
List<Integer> integers = Arrays.asList(1, 2, 3);
List<Double> doubles = Arrays.asList(1.1, 2.2, 3.3);
printNumbers(integers);
printNumbers(doubles);
System.out.println("\nKey Features:");
System.out.println("- Type safety at compile time");
System.out.println("- Eliminates casting");
System.out.println("- Better code reusability");
System.out.println("- Wildcards for flexibility");
System.out.println("\nBenefits:");
System.out.println("- Catch type errors at compile time");
System.out.println("- No runtime ClassCastException");
System.out.println("- More readable code");
System.out.println("- Better IDE support");
}
// Generic method
public static <T> void printArray(T[] array)
{
for (T element : array)
{
System.out.println(" " + element);
}
}
// Method with upper bounded wildcard
public static void printNumbers(List<? extends Number> numbers)
{
for (Number num : numbers)
{
System.out.println(" " + num);
}
}
}
// Generic class
class Box<T>
{
private T item;
public void setItem(T item)
{
this.item = item;
}
public T getItem()
{
return item;
}
}