0% found this document useful (0 votes)
2 views

Lab Manual - Lab 19-Generics

The document outlines a lab exercise for CSE 215L at North South University focused on understanding Generics in Java. It emphasizes the benefits of Generics, including code reusability, type safety, elimination of type casting, and improved readability. The lab tasks involve creating a generic class 'Storage<T>' and a static generic method 'swap()' in a class 'Utility' to demonstrate these concepts.

Uploaded by

aishaabedin0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual - Lab 19-Generics

The document outlines a lab exercise for CSE 215L at North South University focused on understanding Generics in Java. It emphasizes the benefits of Generics, including code reusability, type safety, elimination of type casting, and improved readability. The lab tasks involve creating a generic class 'Storage<T>' and a static generic method 'swap()' in a class 'Utility' to demonstrate these concepts.

Uploaded by

aishaabedin0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

North South University

Department of Electrical and Computer Engineering


CSE 215L: Programming Language II Lab

Lab – 19: Generics in Java

Objective:
● To understand Generics

Why use Generics?

● Code Reusability: Generics allow you to create a method, class, or interface once and
reuse it with different data types.
● Type Safety: By using generics, type-related errors are caught at compile time, making
the code more robust and secure.
● Elimination of Type Casting: Generics handle type conversion automatically, removing
the need for manual type casting when accessing data.
● Improved Readability and Maintainability: Specifying types enhances code clarity
and simplifies future modifications.

Task:

1. Generic Class:

Create a generic class Storage<T> with a private attribute value, methods to set and
get its value, and test it in a main method.

2. Generic Method:

Create a class Utility with a static generic method swap() that swaps two values of the
same type and prints the values before and after the swap. Test it in a main method.
//1. Generic Class
class Storage<T> {
private T value;

public void setValue(T value) {


this.value = value;
}

public T getValue() {
return value;
}
}

public class Main {


public static void main(String[] args) {
// Testing Storage class
Storage<Integer> integerStorage = new Storage<>();
integerStorage.setValue(42);
System.out.println("Stored Integer: " + integerStorage.getValue());

Storage<String> stringStorage = new Storage<>();


stringStorage.setValue("Hello, Generics!");
System.out.println("Stored String: " + stringStorage.getValue());
}
}

//2. Generic Method


class Utility {
public static <T> void swap(T first, T second) {
System.out.println("Before Swap: First = " + first + ", Second = " + second);
T temp = first;
first = second;
second = temp;
System.out.println("After Swap: First = " + first + ", Second = " + second);
}
}

public class Main {


public static void main(String[] args) {
// Testing Utility.swap()
Integer first = 10;
Integer second = 20;
Utility.swap(first, second);

String str1 = "Apple";


String str2 = "Orange";
Utility.swap(str1, str2);
}
}

You might also like