Deep copy and shallow copy in Java

Source: Internet
Author: User

Have never heard of these two nouns, these days with the list and map of the time to find the bug, only for the first time on the StackOverflow know there are so two simple terminology can describe the problems I encountered.

Written down is based on their own understanding, perhaps inaccurate, if later found not to come back to fix.

First, there are only two types of data in Java: Primitive type and object type.

The basic data type (primitive type) is assigned with only one case, because their memory location is the location of their actual value, and if one variable is assigned to another variable, a new value is added to the memory.

There are two cases of a reference type (object), because the object itself has two values. One is the memory address and the other is the value of the object itself that this memory address holds. When you assign a variable to another variable, there are two things:

1. Copy only the memory address to the new variable, in which case two variables point to the same memory location, and any one variable modifies it to affect the other variable. This is called the shallow copy.

2. Re-generate the value of object, that is, two variables, although the values are equal, but in memory has a different position, each other does not affect each other. This is deep copy.

In fact, I do not want to get what circumstances to shallow copy, it seems to be an extra variable AH.

Deep copy and shallow copy in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.