Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
There are two different strategies to copy an object: shallow and deep copies. A shallow copy can be made to copy the object without any of its contents or data. Rather, all the variables are passed by reference into the copied object. After a shallow copy of an object has been created, the objects within the original and copy objects refer to the same data and memory. Thus, modifying the original object’s contents also modifies the copied object. By default, calling the super.clone() method against an object performs a shallow copy. The shallowCopyClone() method in the solution to this recipe demonstrates this technique. Chapter 5 ObjeCt-Oriented java 186 The second type of copy that can be made is a deep copy, which copies the object, including all the contents. Therefore, each object refers to a different space in memory, and modifying one object does not affect the other. The difference between a deep and a shallow copy is demonstrated in the solution to this recipe. First, team1 and team2 objects are created. Next, they are populated with some values. The team3 object is then set equal to the team1 object, and the team4 object is made a clone of the team2 object. When the values are changed within the team1 object, they are also changed in the team3 object because both objects’ contents refer to the same space in memory. This is an example of a shallow copy of an object. When the values are changed within the team2 object, they remain unchanged in the team4 object because each object has its own variables that refer to different spaces in memory. This is an example of a deep copy. To make an exact copy of an object (deep copy), you must serialize the object so that it can be written to disk. The base Object class implements the clone() method. By default, the Object class’s clone() method is protected. To make an object cloneable, it must implement the Cloneable interface and override the default clone() method. You can make a deep copy of an object by serializing it through a series of steps, such as writing the object to an output stream and then reading it back via an input stream. The steps shown in the clone() method of the solution to this recipe do just that. The object is written to a ByteArrayOutputStream and then read using a ByteArrayInputStream. Once that has occurred, the object has been serialized, which creates the deep copy. The clone() method in the solution to this recipe has been overridden to create a deep copy. Once these steps have been followed and an object implements Cloneable and overrides the default object clone() method, it is possible to clone the object. To make a deep copy of an object, simply call that object’s overridden clone() method as seen in the solution. If you were to simply return the object from the clone() method, there would need to be a typecast, as shown in the following. Team team4 = (Team) team2.clone(); Cloning objects is not very difficult, but understanding the differences that can vary with object copies is important. Chapter 5 ObjeCt-Oriented java |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling