在Java中,什么是浅拷贝?

发布于 2024-07-29 14:58:22 字数 146 浏览 7 评论 0原文

java.util.Calendar.clone() 返回“...具有相同属性的新日历”并返回“此日历的浅表副本”。

这似乎不是回答的浅拷贝

在Java中,什么是浅拷贝?

它与 Java 深拷贝(如果存在)有何不同?

java.util.Calendar.clone() returns "...a new Calendar with the same properties" and returns "a shallow copy of this Calendar".

This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does not seem to follow the language agnostic definition. As I step through the code I notice that the structure and the elements are copied to this new object, more than the language agnostic structure only.

In Java, what is a shallow copy?

How does it differ from a Java deep copy (if that exists)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

少女的英雄梦 2024-08-05 14:59:28

浅拷贝:在此克隆中,对克隆对象的任何更改也会反映到原始对象。

深度复制:在此克隆中,会分配单独的克隆内存,这意味着对克隆对象的任何更改都不会反映到原始对象。

Shallow Copy : In this cloning any changes to Cloned Object is reflected to Original Object also.

Deep Copy : In this cloning a separate cloned memory is alloted which means any changes to cloned object will not be reflected to original object.

神经大条 2024-08-05 14:59:22

在浅拷贝中,克隆对象具有原始值的副本,但对象引用引用与原始副本相同的对象。
浅拷贝有一个显着的缺点,克隆对象和原始副本引用相同的地址对象。 克隆对象在地址对象中所做的任何更改也将反映在原始副本中,这是一种不需要的行为。 我们真正想要的是用户对象的两个单独的副本。 深度复制可以解决这种情况。

深度复制不仅克隆原始值,还创建对象引用的副本。

您可以在此处查看有关此问题的工作示例:https: //codingninjaonline.com/2017/11/09/deep-vs-shallow-copy/

In a shallow copy,the clone object has a copy of primitive values but the object references refer to the same objects as the original copy.
Shallow Copies have a significant drawback, cloned object and original copy refer to the same address object. Any change that cloned object makes in address object will also be reflected in original copy, which is an unwanted behaviour. What we really wanted is two separate copies of user object. Deep copying comes to our rescue for this kind of situation.

Deep copying clones not just the primitive values, it also creates copies of object references.

You can have a look at working example on this at here :https://codingninjaonline.com/2017/11/09/deep-vs-shallow-copy/

灼疼热情 2024-08-05 14:59:16

浅拷贝只是将对象引用复制到目标引用中。 它不会在堆上创建新对象。
默认情况下,Java 使用clone() 函数进行浅克隆。

为了在堆上获取一个新对象,必须执行深度克隆,这可以通过序列化和反序列化来实现。

A shallow copy just copies the object reference into the target reference. It does not create a new object on the heap.
By default, Java does shallow cloning using clone() function.

To get a new object on the heap, one has to perform deep cloning which can be implemented by Serialization and De-serialization.

情深缘浅 2024-08-05 14:59:11

首先,如果我们谈论一维数组,ArrayList 的 Javadoc 就有些错误,因为它使用了数组中的 copyOf 方法。 所以clone()返回一个一维副本,至少从1.5开始(我没有进一步测试)! 这就是 Java 中“浅层”的含义:一维

您可以在此处阅读更多内容: http://www.javapractices.com/topic/TopicAction.do?Id=3。 所以clone()不是浅拷贝! 如果你想要一维数组的真正浅拷贝,只需引用它:

Array a = new Array();
Array b = a;                    //a is only a shallow copy, nice for synchronisation

Java 中的数组很棘手,也是因为 Java 确实是按值传递的,但数组的值只是它们的指针! 另一方面,这允许我们同步对象,这是一件很棒的事情。 尽管如此,如果您在数组(或 ArrayList)中使用数组,仍然会出现一些问题,因为容器数组(或 ArrayList)的 clone() 不会复制它们的值,只会复制它们的引用! 所以你根本不应该将任何数组放入数组中,你应该只处理数组中的对象!

而且 Javadoc 有时很难理解,所以尝试一下测试...

玩得开心!

First of all, the Javadoc of ArrayList is somewhat wrong if we are talking about one-dimensional arrays, as it uses the method copyOf in Arrays. So clone() gives back a one-dimensional copy, at least since 1.5 (I didn't test further)! So that's what "shallow" means in Java: one-dimensional

You can read more here: http://www.javapractices.com/topic/TopicAction.do?Id=3. So clone() is no shallow copy! If you want a real shallow copy of a one-dimensional array, just reference it:

Array a = new Array();
Array b = a;                    //a is only a shallow copy, nice for synchronisation

Arrays in Java are tricky, also because Java does pass-by-value, but the values of arrays are only their pointers! In the other hand this allows us to synchronize the objects, which is a great thing. Still, there are some problems if you use arrays within arrays (or ArrayLists), because a clone() of the container array (or ArrayList) won't copy their values, only their references! So you simply shouldn't put any arrays into an array, you should only deal with objects in an array!

And Javadoc is difficult to understand sometimes, so give testing a try...

Have fun!

兰花执着 2024-08-05 14:59:05

您从哪里获得此文档?

java.sun.com 上的官方 Java 6 文档只有 Calendar.clone()< /a> 返回对象的副本。 没有提到浅薄。

更一般地说,Java 中的浅复制是一种获得新对象引用的复制,但新对象(直接或间接)保存对原始对象中数据的引用。

例如:

class MyClass{
  private List<Integer> innerList;

  public MyClass(List<Integer> list) { innerList = list; }

  //Some code...

  public Object clone(){
    return new MyClass(innerList);
  }
}

在其clone()中返回一个浅拷贝。

Where are you getting this documentation?

The official Java 6 docs on java.sun.com simply have Calendar.clone() returning a copy of the object. No mention of shallow.

More generally, a shallow copy in Java is one where you get a new object reference but the new object holds (directly or indirectly) references to data in the original.

For example:

class MyClass{
  private List<Integer> innerList;

  public MyClass(List<Integer> list) { innerList = list; }

  //Some code...

  public Object clone(){
    return new MyClass(innerList);
  }
}

returns a shallow copy in its clone().

风情万种。 2024-08-05 14:58:59

浅拷贝是通过引用传递......
深度复制是按值传递……
上下文不同,但过程完全相同。 首先记住,在Java方法调用中,基元是按值传递的,而对象是按引用传递的(在其他语言中,对象也可能是按值传递的)。 现在,在Java中,当调用者将原语传递给被调用方法时,被调用方法只是将其克隆为新的局部变量,这是一个深层副本。 而如果传递一个对象,被调用的方法只会对同一对象进行新的本地引用,这是浅复制。 如果您了解调用,您就了解深/浅复制,& 反之亦然。

SHALLOW COPY is PASS-BY-REFERENCE ...
DEEP COPY is PASS-BY-VALUE …
The context is different, but the process is exactly the same. First remember that in Java method calling, primitives are passed by value, while objects are passed by reference (in other languages, objects may also be passed by value). Now, in Java, when the caller passes a primitive to the called method, the called method simply clones it as a new local variable, which is a deep copy. While if an object is passed, the called method will only make a new local reference to the same object, which is shallow copy. If you understand calling, you understand deep/shallow copy, & vice versa.

寄意 2024-08-05 14:58:54

这似乎是文档中的一个错误。 我不明白 Android 的 Calendar.clone 方法所做的任何事情如何满足“浅拷贝”的典型定义(在 Java 或其他中)。

It appears to be a mistake in the documentation. I don't see how anything that Android's Calendar.clone method does meets the typical definition (in Java or otherwise) of a "shallow copy".

你与昨日 2024-08-05 14:58:50

1.6 文档将 Calendar.clone 描述为“创建并返回此对象的副本”。 由 Object.clone 指定的文字浅拷贝没有任何意义。 Java 在相当典型的意义上使用术语“浅拷贝”。

The 1.6 docs document Calendar.clone as "Creates and returns a copy of this object." A literal shallow copy as specified by Object.clone wouldn't make any sense. Java uses the term "shallow copy" in a fairly typical sense.

十雾 2024-08-05 14:58:44

浅拷贝是对象引用指针的拷贝,而深拷贝是对象本身的拷贝。 在Java中,对象保存在后台,处理对象时通常交互的是指针。 变量名指向对象的内存空间。 当您将一个变量设置为等于另一个变量时,就会创建浅复制,如下所示:

Object B = A;

可以通过获取对象 A 的属性并将它们放入新对象 B 中来创建深复制。

Object B = new Object(A.getProperty1(), A.getProperty2()...);

这会影响程序行为,因为如果您创建浅复制并对其执行一项任务,该任务会影响该对象的所有浅副本。 如果您对深层副本进行更改,则只有该副本受到影响。 我希望这对您来说足够详细。

A shallow copy is a copy of the reference pointer to the object, whereas a deep copy is a copy of the object itself. In Java, objects are kept in the background, what you normally interact with when dealing with the objects is the pointers. The variable names point to the memory space of the object. A shallow copy is made when you set one variable equal to another like so:

Object B = A;

A deep copy could be made by getting the properties of object A and putting them in a new object B.

Object B = new Object(A.getProperty1(), A.getProperty2()...);

This affects program behavior in that if you make a shallow copy and perform a task on it, that affects all shallow copies of the object. If you make a change to a deep copy, only that copy is affected. I hope this is detailed enough for you.

同尘 2024-08-05 14:58:40

浅拷贝只是一组指向相同内存位置的指针。 实际上它不会创建真正的副本,因此内存使用量较低。

在深度复制的情况下,会创建内存段的精确副本,并将指针设置为新的内存位置。 因此理论上,这种情况下的内存消耗应该是两倍。

Shallow copy is a just a set of pointers to the same memory locations. Actually it does not create a real copy so the memory usage is lower.

In a case of a deep copy, an exact copy of the memory segment is created and pointers are set to new memory locations. So theoritically the memory consumption should be twice in this case.

顾忌 2024-08-05 14:58:36

浅拷贝只是复制类中引用的值。 深拷贝复制值。 给定:

class Foo {
  private Bar myBar;
  ...
  public Foo shallowCopy() {
    Foo newFoo = new Foo();
    newFoo.myBar = myBar;
    return newFoo;
  }

  public Foo deepCopy() {
    Foo newFoo = new Foo();
    newFoo.myBar = myBar.clone(); //or new Bar(myBar) or myBar.deepCopy or ...
    return newFoo;
  }
}

Foo myFoo = new Foo();  
Foo sFoo = myFoo.shallowCopy();  
Foo dFoo = myFoo.deepCopy();  

myFoo.myBar == sFoo.myBar => true  
myFoo.myBar.equals(sFoo.myBar) => true  
myFoo.myBar == dFoo.myBar => **false**  
myFoo.myBar.equals(dFoo.myBar) => true  

在这种情况下,浅复制具有相同的引用(==),而深复制仅具有等效引用(.equals())。

如果对浅复制引用的值进行更改,则副本会反映该更改,因为它共享相同的引用。 如果对深度复制引用的值进行更改,则副本不会反映该更改,因为它不共享相同的引用。

C主义

int a = 10; //init
int& b = a; //shallow - copies REFERENCE
int c = a;  //deep - copies VALUE
++a;

结果:

a is 11  
*b is 11  
c is 10

A shallow copy just copies the values of the references in the class. A deep copy copies the values. given:

class Foo {
  private Bar myBar;
  ...
  public Foo shallowCopy() {
    Foo newFoo = new Foo();
    newFoo.myBar = myBar;
    return newFoo;
  }

  public Foo deepCopy() {
    Foo newFoo = new Foo();
    newFoo.myBar = myBar.clone(); //or new Bar(myBar) or myBar.deepCopy or ...
    return newFoo;
  }
}

Foo myFoo = new Foo();  
Foo sFoo = myFoo.shallowCopy();  
Foo dFoo = myFoo.deepCopy();  

myFoo.myBar == sFoo.myBar => true  
myFoo.myBar.equals(sFoo.myBar) => true  
myFoo.myBar == dFoo.myBar => **false**  
myFoo.myBar.equals(dFoo.myBar) => true  

In this case the shallow copy has the same reference (==) and the deep copy only has an equivalent reference (.equals()).

If a change is made to the value of a shallowly copied reference, then the copy reflects that change because it shares the same reference. If a change is made to the value of a deeply copied reference, then the copy does not reflect that change because it does not share the same reference.

C-ism

int a = 10; //init
int& b = a; //shallow - copies REFERENCE
int c = a;  //deep - copies VALUE
++a;

Result:

a is 11  
*b is 11  
c is 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文