Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the deep vs shallow copies of java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to understand the deep vs shallow copies of java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to understand the deep vs shallow copies of java"!

1. Background

When discussing deep copy and shallow copy, you need to understand that it is certainly not the basic data type or String, because they are immutable and are all value passes. In other words, when we talk about deep copy or shallow copy, we all talk about reference types, and we're talking about how references are passed.

two。 Fundamental difference

See if the reference points to the same object

Shallow copydeep copy distinguishing reference points to the same object reference performs different object feature modification one object will affect the other object modification one object will not affect the other object 3. Examples

First define the person class and override the hashCode method (as for why you rewrite HashCode, refer to HashCode)

/ * * @ author shengjk1 * @ date 2019-9-24 * / public class Person implements Serializable {private static final long serialVersionUID = 5866287941609270803 private int age;private String name;private int grade;public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name } public int getGrade () {return grade;} public void setGrade (int grade) {this.grade = grade;} static void run () {System.out.println ("run....");} @ Overridepublic boolean equals (Object o) {if (this = = o) return true;if (o = = null | | getClass ()! = o.getClass () return false; Person person = (Person) o Return getAge () = = person.getAge () & & getGrade () = = person.getGrade () & & Objects.equals (getName (), person.getName ());} @ Overridepublic int hashCode () {return Objects.hash (getAge (), getName (), getGrade ());} @ Overridepublic String toString () {return "Person {" + "age=" + age + ", name='" + name +'\'+ ", grade=" + grade +'}';}

Main method

HashMap map= new HashMap (); Person person = new Person (); person.setAge (1); person.setName ("a"); person.setGrade (1); map.put ("a", person); System.out.println ("map=") System.out.println ("before modification" + map.get ("a")); person.setName ("aa"); System.out.println ("after modification" + map.get ("a")); before map= modification Person {age=1, name='a', grade=1} modified Person {age=1, name='aa', grade=1}

Naming the attributes in the modified person, why did the person in map also change?

Both the local variable person and map actually refer to the same Person object. Therefore, when some properties in the Person object change, the local variables person and map are perceptible.

So how can we not "interfere with each other", as long as the objects they refer to are inconsistent.

4. Implement deep copy

Currently, (jdk8) java itself does not support an implementation of deep copy. We can do it ourselves.

Override the clone method, too complex

Serialization and deserialization is simple

Apache common lang has been implemented.

HashMap deelClone = SerializationUtils.clone (map); person.setName ("b"); System.out.println ("deelClone.get (\" a\ ") =" + deelClone.get ("a"); System.out.println ("map.get (\" a\ ") =" + map.get ("a") DeelClone.get ("a") = Person {age=1, name='aa', grade=1} map.get ("a") = Person {age=1, name='b', grade=1}

Person name has not changed since deep clone

At this point, I believe you have a deeper understanding of "how to understand the deep vs shallow copies of java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report