WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Expert · question 63 of 100

What is the difference between a shallow copy and a deep copy in Java Collections, and when would you use one over the other?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

In Java Collections, when we talk about copying an object, there are two types of copying - shallow copying and deep copying.

Shallow Copying:

In shallow copying, a new object is created with the same reference as the original object. The new object points to the same memory location as the original object. In other words, the new object is just a copy of the original object’s reference. Any changes made to the original object will be reflected in the new object, and vice versa. Shallow copying can be useful when you need to create a copy of an object quickly without actually copying its contents.

Here’s an example of shallow copying a list:

List<String> originalList = new ArrayList<>();
originalList.add("apple");
originalList.add("banana");
originalList.add("orange");

List<String> newList = new ArrayList<>(originalList);

In the above code, we created a new list newList using the ArrayList constructor that takes a collection as an argument. This constructor creates a new list and adds all the elements of the original list to it. This is an example of shallow copying. If we add or remove elements from either originalList or newList, the changes will be reflected in both lists.

Deep Copying:

In deep copying, a new object is created with a new memory location. The new object has the same value as the original object, but is completely independent of the original object. In other words, deep copying creates a completely new object with its own memory space, and copies all the data from the original object to the new object. Deep copying can be useful when you need to create a copy of an object that you can modify independently of the original object.

Here’s an example of deep copying a list:

List<String> originalList = new ArrayList<>();
originalList.add("apple");
originalList.add("banana");
originalList.add("orange");

List<String> newList = new ArrayList<>();
for (String item : originalList) {
    newList.add(item);
}

In the above code, we created a new list newList by iterating over originalList and adding each element to the new list. This is an example of deep copying. If we add or remove elements from either originalList or newList, the changes will not be reflected in the other list.

In general, shallow copying is faster and simpler than deep copying, but it can lead to unexpected behavior if you’re not careful. Deep copying is safer and more reliable, but can be slower and more complex, especially for complex objects. Which type of copying to use depends on your specific use case and requirements.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics