জাভা জেনেরিক্স প্রোগ্রামিংয়ের একটি শক্তিশালী ফিচার যা Code Reusability বৃদ্ধি করে এবং Type Safety নিশ্চিত করে। Generics ব্যবহার করে একাধিক ডেটা টাইপের জন্য একক কোড লিখে পুনঃব্যবহারযোগ্য (Reusable) সমাধান তৈরি করা যায়।
Generics এর মাধ্যমে Code Reusability কিভাবে বৃদ্ধি পায়?
- একটি কোডে একাধিক টাইপের জন্য কাজ করা সম্ভব:
- একই ক্লাস বা মেথড বিভিন্ন ডেটা টাইপের সাথে কাজ করতে পারে।
- নতুন ক্লাস বা মেথড লেখার প্রয়োজন কমে যায়।
- Compile-Time Type Checking:
- টাইপ-সেইফ কোড তৈরি হয়, যা টাইপ-কাস্টিং এর ঝামেলা কমায়।
- কম্পাইল-টাইমে ভুল ধরা পড়ে, ফলে রানটাইম ত্রুটি এড়ানো যায়।
- কোড সংক্ষিপ্ত এবং সহজপাঠ্য:
- জেনেরিক্স ব্যবহার করে টাইপ-স্পেসিফিক ক্লাস বা মেথডগুলোর সংখ্যা কমানো যায়।
Generics এর ব্যবহার: উদাহরণসমূহ
1. Generics ক্লাস দিয়ে Code Reusability
class Box<T> {
private T item;
public void setItem(T item) {
this.item = item;
}
public T getItem() {
return item;
}
}
public class GenericClassExample {
public static void main(String[] args) {
// Integer টাইপের জন্য
Box<Integer> integerBox = new Box<>();
integerBox.setItem(10);
System.out.println("Integer Value: " + integerBox.getItem());
// String টাইপের জন্য
Box<String> stringBox = new Box<>();
stringBox.setItem("Hello Generics");
System.out.println("String Value: " + stringBox.getItem());
}
}
আউটপুট:
Integer Value: 10
String Value: Hello Generics
2. Generics মেথড দিয়ে Code Reusability
public class GenericMethodExample {
// জেনেরিক মেথড
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] stringArray = {"Java", "Generics", "Are", "Powerful"};
// Integer Array প্রিন্ট করা
printArray(intArray);
// String Array প্রিন্ট করা
printArray(stringArray);
}
}
আউটপুট:
1 2 3 4 5
Java Generics Are Powerful
3. Generics এবং Collections এর মাধ্যমে Code Reusability
import java.util.ArrayList;
import java.util.List;
public class GenericCollectionsExample {
public static void main(String[] args) {
// Integer List
List<Integer> integerList = new ArrayList<>();
integerList.add(10);
integerList.add(20);
integerList.add(30);
// String List
List<String> stringList = new ArrayList<>();
stringList.add("Java");
stringList.add("Generics");
stringList.add("Example");
// Generic মেথড ব্যবহার করে প্রিন্ট করা
printList(integerList);
printList(stringList);
}
public static <T> void printList(List<T> list) {
for (T element : list) {
System.out.print(element + " ");
}
System.out.println();
}
}
আউটপুট:
10 20 30
Java Generics Example
4. Multiple Type Parameters দিয়ে Code Reusability
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
public class MultipleGenericsExample {
public static void main(String[] args) {
Pair<String, Integer> pair = new Pair<>("Age", 30);
System.out.println("Key: " + pair.getKey() + ", Value: " + pair.getValue());
Pair<Integer, String> anotherPair = new Pair<>(101, "John Doe");
System.out.println("Key: " + anotherPair.getKey() + ", Value: " + anotherPair.getValue());
}
}
আউটপুট:
Key: Age, Value: 30
Key: 101, Value: John Doe
Generics এর সুবিধা
- Reusable Code: একবার কোড লিখে তা বিভিন্ন ডেটা টাইপের জন্য ব্যবহার করা যায়।
- Type Safety: টাইপ-সেইফ কোড নিশ্চিত করা হয়।
- Readable Code: কম এবং সহজ কোড।
- Compile-Time Checking: টাইপ সম্পর্কিত ত্রুটি কম্পাইল-টাইমেই ধরা যায়।
- Collections Framework Integration: Generics Collections-এর সাথে সহজে কাজ করে।
Generics ব্যবহার না করলে সমস্যাগুলি
Type Casting এর ঝুঁকি:
- টাইপ-কাস্টিং এর সময় রানটাইম ত্রুটি হতে পারে।
List list = new ArrayList(); list.add("Hello"); Integer num = (Integer) list.get(0); // ClassCastException- Code Duplication:
- প্রতিটি টাইপের জন্য আলাদা ক্লাস বা মেথড তৈরি করতে হয়।
জেনেরিক্স প্রোগ্রামিংয়ের একটি অত্যন্ত কার্যকরী টুল যা Code Reusability এবং Type Safety নিশ্চিত করে। জেনেরিক ক্লাস, মেথড, এবং ইন্টারফেস ব্যবহার করে একাধিক টাইপের জন্য একক কোড তৈরি করা যায়। এটি কোডের সংক্ষিপ্ততা, কার্যকারিতা, এবং স্থায়িত্ব বাড়াতে গুরুত্বপূর্ণ ভূমিকা পালন করে।
Content added By
Read more