Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deep and Shallow Comparison!!

 
Ravi Kumar Ravuru
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,
What is difference between deep and shallow comparison?Which of the two comparisons are used in equals() method of Object?Please help me out.
Regards,
Ravi
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually "deep" and "shallow" are used to refer to copying, not comparison. But by analogy, "deep" comparison would mean comparing two objects, and all the objects they reference as members, and all the objects they reference, etc. "Shallow" comparison, in contrast, would mean just comparing two objects, I suppose.
Object.equals() does a "shallow" comparison, I suppose, but this is only because Object has no data members. Most implementations of equals() should do "deep" comparisons.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ernest said, the reference is usually to "deep" and "shallow" copies.
A shallow copy of an ArrayList would make a copy of the ArrayList object, but both ArrayLists would end up referencing the same objects as elements of the List. In other words, after a shallow copy the folloowing two statements should be true:
originalList != copyOfList
originalList.get(0) == copyOfList.get(0)

By contrast, a deep copy of an ArrayList would not only copy the ArrayList object, but make copies of each of the elements as well (and copies of objects contained in each element object ad infinitum). After a deep copy, the collowing statements should hold true:
originalList != copyOfList
originalList.get(0) != copyOfList.get(0)
originalList.get(0).equals(copyOfList.get(0))

From this you can probably intuitively understand the differences between shallow and deep comparisons. I guess a shallow comparison of an two ArrayLists would only compare the actual ArrayList objects, whereas a deep comparison would compare the ArrayList objects as well as each element of the ArrayLists.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic