0% found this document useful (0 votes)
28 views6 pages

Java Generics and Object Copying Techniques

The document discusses generic functions and classes in Java, highlighting their role in providing type safety and code reusability through templates. It also explains the concepts of shallow copy and deep copy, detailing how shallow copies only duplicate references while deep copies create independent copies of objects. Examples are provided to illustrate the implementation of generics and the differences between shallow and deep copying.

Uploaded by

salman.488215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Java Generics and Object Copying Techniques

The document discusses generic functions and classes in Java, highlighting their role in providing type safety and code reusability through templates. It also explains the concepts of shallow copy and deep copy, detailing how shallow copies only duplicate references while deep copies create independent copies of objects. Examples are provided to illustrate the implementation of generics and the differences between shallow and deep copying.

Uploaded by

salman.488215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT :2

Topic: Generic Functions/Classes,


Shallow Copy & Deep Copy in Java

Submitted By: Muhammad Moez

Roll number: 24014198-048


Section D Evening

Submitted to: Mr. M. Attique Rafique


GENERIC FUNCTIONS/CLASSES, SHALLOW COPY
& DEEP COPY
1. Generic Functions and Generic Classes
(Templates)
Generics were introduced in Java 5 to provide stronger
type checks at compile time and eliminate the risk of
runtime ClassCastException. By using generics,
programmers can write flexible and reusable code that
works with different data types while maintaining type
integrity. For example, instead of creating separate
classes for IntegerList, StringList, etc., a
single List<T> can handle all types. This also reduces the
need for explicit type casting, making the code cleaner
and less [Link] in Java allow you to create
classes, interfaces, and methods that operate on type
parameters rather than specific data types. This
promotes type safety and code reusability.
 Generic Class: A class that can work with any data
type.
 Generic Method: A method that can accept
parameters of different types.

Example Generic Class:


class Box<T> {
private T content;
public void setContent(T content) {
[Link] = content;
}
public T getContent() {
return content;
}
}
public class Main {
public static void main(String[] args) {
Box<String> stringBox = new Box<>();
[Link]("Generics Example");
[Link]([Link]());
Box<Integer> intBox = new Box<>();
[Link](123);
[Link]([Link]());
}
}

Example Generic Method:


public class Main {
public static <T> void printArray(T[] array) {
for (T element : array) {
[Link](element + " ");
}
[Link]();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3};
String[] stringArray = {"A", "B", "C"};
printArray(intArray);
printArray(stringArray);
}
}

2. Shallow Copy vs Deep Copy


In Java, the default clone() method performs a shallow
copy, meaning if an object contains references to other
objects, only the references are copied, not the referred
objects themselves. This can lead to unintended side
effects if the copied object modifies shared references.
A deep copy, on the other hand, ensures complete
independence by recursively copying all nested objects.
Deep copying is essential when working with mutable
objects to maintain data integrity.
 Shallow Copy: Copies only the references (memory
addresses) of objects, not the actual objects.
Changes in one object affect the other.
 Deep Copy: Creates a new copy of the object and all
objects referenced within it. Changes in one object
do not affect the other.

Example: Shallow Copy


class Person {
String name;
Person(String name) {
[Link] = name;
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Alice");
Person p2 = p1;
[Link] = "Bob";
[Link]([Link]);
}
}
Example: Deep Copy
class Person implements Cloneable {
String name;
Person(String name) {
[Link] = name;
}
protected Object clone() throws
CloneNotSupportedException {
return [Link]();
}
}
public class Main {
public static void main(String[] args) throws
CloneNotSupportedException {
Person p1 = new Person("Alice");
Person p2 = (Person) [Link]();
[Link] = "Bob";
[Link]([Link]);
}
}

You might also like