浅析java的浅拷贝和深拷贝
Java中任何实现了Cloneable接口的类都可以通过调用clone()方法来复制一份自身然后传给调用者。一般而言,clone()方法满足:
(1) 对任何的对象x,都有x.clone() !=x,即克隆对象与原对象不是同一个对象。
(2) 对任何的对象x,都有x.clone().getClass()==x.getClass(),即克隆对象与原对象的类型一样。
(3) 如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。
/**
* Creates and returns a copy of this object. The precise meaning
* of "copy" may depend on the class of the object. The general
* intent is that, for any object {@code x}, the expression:
* <blockquote>
* <pre>
* x.clone() != x</pre></blockquote>
* will be true, and that the expression:
* <blockquote>
* <pre>
* x.clone().getClass() == x.getClass()</pre></blockquote>
* will be {@code true}, but these are not absolute requirements.
* While it is typically the case that:
* <blockquote>
* <pre>
* x.clone().equals(x)</pre></blockquote>
* will be {@code true}, this is not an absolute requirement.
* <p>
* By convention, the returned object should be obtained by calling
* {@code super.clone}. If a class and all of its superclasses (except
* {@code Object}) obey this convention, it will be the case that
* {@code x.clone().getClass() == x.getClass()}.
* <p>
* By convention, the object returned by this method should be independent
* of this object (which is being cloned). To achieve this independence,
* it may be necessary to modify one or more fields of the object returned
* by {@code super.clone} before returning it. Typically, this means
* copying any mutable objects that comprise the internal "deep structure"
* of the object being cloned and replacing the references to these
* objects with references to the copies. If a class contains only
* primitive fields or references to immutable objects, then it is usually
* the case that no fields in the object returned by {@code super.clone}
* need to be modified.
* <p>
* The method {@code clone} for class {@code Object} performs a
* specific cloning operation. First, if the class of this object does
* not implement the interface {@code Cloneable}, then a
* {@code CloneNotSupportedException} is thrown. Note that all arrays
* are considered to implement the interface {@code Cloneable} and that
* the return type of the {@code clone} method of an array type {@code T[]}
* is {@code T[]} where T is any reference or primitive type.
* Otherwise, this method creates a new instance of the class of this
* object and initializes all its fields with exactly the contents of
* the corresponding fields of this object, as if by assignment; the
* contents of the fields are not themselves cloned. Thus, this method
* performs a "shallow copy" of this object, not a "deep copy" operation.
* <p>
* The class {@code Object} does not itself implement the interface
* {@code Cloneable}, so calling the {@code clone} method on an object
* whose class is {@code Object} will result in throwing an
* exception at run time.
*
* @return a clone of this instance.
* @throws CloneNotSupportedException if the object's class does not
* support the {@code Cloneable} interface. Subclasses
* that override the {@code clone} method can also
* throw this exception to indicate that an instance cannot
* be cloned.
* @see java.lang.Cloneable
*/
protected native Object clone() throws CloneNotSupportedException;
首先来看看浅拷贝和深拷贝的定义:
浅拷贝:使用一个已知实例对新创建实例的成员变量逐个赋值,这个方式被称为浅拷贝。
深拷贝:当一个类的拷贝构造方法,不仅要复制对象的所有非引用成员变量值,还要为引用类型的成员变量创建新的实例,并且初始化为形式参数实例值。这个方式称为深拷贝
也就是说浅拷贝只复制一个对象,传递引用,不能复制实例。而深拷贝对对象内部的引用均复制,它是创建一个新的实例,并且复制实例。
对于浅拷贝当对象的成员变量是基本数据类型时,两个对象的成员变量已有存储空间,赋值运算传递值,所以浅拷贝能够复制实例。但是当对象的成员变量是引用数据类型时,就不能实现对象的复制了。
存在一个对象Person,代码如下:

public class Person {
private String name;
private String sex;
private int age;
public Person(String name,String sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public Person(Person p){ //拷贝构造方法,复制对象
this.name = p.name;
this.sex = p.sex;
this.age = p.age;
}
}

上面的对象Person有三个成员变量。name、sex、age。两个构造方法。第二个的参数为该对象,它称为拷贝构造方法,它将创建的新对象初始化为形式参数的实例值,通过它可以实现对象复制功能。
又有一个对象Asian,如下:

public class Asian {
private String skin;
Person person;
public Asian(String skin,Person person){
this.skin = skin;
this.person = person; //引用赋值
}
public Asian(Asian asian){ //拷贝构造方法,复制对象
this(asian.skin,asian.person);
}
}

上面对象也存在着两个成员变量,skin 和Person对象
对于person对象有如下:
Person p1 = new Person("李四","mam",23);
Person p2 = new Person(P1);
当调用上面的语句时。P2对象将会对P1进行复制。执行情况如下如下图:

对于Asian对象有:
Asian a1 = new Asian("yellow",new Person("李四","mam",23));
Asian a2 = new Asian(a1);
New Asian(a1)执行Asian类的拷贝构造方法,由于对象赋值是引用赋值。使得a1和a2引用同一个对象
如下图:

当a1执行某条可以改变该值的语句时,那么a1将会通过这个语句也可以改变a2对象的成员变量
如果执行以下语句:a2.name = new Person(a1.name)
这时将会创建一个新的Person对象
如下图:

http://www.cnblogs.com/chenssy/p/3308489.html
浅析java的浅拷贝和深拷贝的更多相关文章
- Java的浅拷贝与深拷贝
Java的浅拷贝与深拷贝 Java中,所有的类都继承Object,Object中有clone方法,它被声明为了 protected ,所以我们但是如果要使用该方法就得重写且声明为public,必须在要 ...
- Java的浅拷贝与深拷贝总结
Java中的对象拷贝(Object Copy)指的是将一个对象的所有属性(成员变量)拷贝到另一个有着相同类类型的对象中去.举例说明:比如,对象A和对象B都属于类S,具有属性a和b.那么对对象A进行拷贝 ...
- 渐析java的浅拷贝和深拷贝
首先来看看浅拷贝和深拷贝的定义: 浅拷贝:使用一个已知实例对新创建实例的成员变量逐个赋值,这个方式被称为浅拷贝. 深拷贝:当一个类的拷贝构造方法,不仅要复制对象的所 ...
- Java中浅拷贝和深拷贝的区别
浅拷贝和深拷贝的定义: 浅拷贝: 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.即对象的浅拷贝会对"主"对象进行拷贝,但不会复制主对象 ...
- 初始JAVA中浅拷贝和深拷贝
1. 简单变量的复制 public static void main(String[] args) { int a = 5; int b = a; System.out.println(a); Sys ...
- Java之浅拷贝与深拷贝
----?浅拷贝 --- 概念 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.简单说,浅拷贝就是只复制所考虑的对象,而不复制它所引用的对象 --- 实现方 ...
- Java之浅拷贝和深拷贝
[概述] Java中的对象拷贝 ( Object Copy ) 是指将一个对象的所有属性(成员变量)拷贝到另一个有着相同类类型的对象中去.例如,对象 A 和对象 B 都属于类 S,具有属性 a 和 b ...
- java的浅拷贝和深拷贝(待解决)
1.什么是浅拷贝,什么是深拷贝? 2.storm的并行度问题,需要使用全局变量static ConcorrentHashMap,因为加了static,所有的线程只能拷贝该全局变量的一个唯一的副本,进行 ...
- 浅析java的深拷贝与浅拷贝
(转自:http://www.cnblogs.com/chenssy/p/3308489.html) 首先来看看浅拷贝和深拷贝的定义: 浅拷贝:使用一个已知实例对新创建实例的成员变量逐个赋值,这个方式 ...
随机推荐
- PHPer的等级划分
PHPer的等级划分 前一段时间刚刚完成PHP的培训,然后想知道自己目前的水平(或者说等级),并且应该在哪些方面进行提高,所以在网上查了一下相关介绍.其中有一篇介绍讲的挺清楚的,至少目前的我还是很认同 ...
- PHP面向对象基础实例
<?phpclass marine{ public $blood = 50; //剩余的血 public $kills = 0; //杀敌数量 static $all_num = 0;//兵的数 ...
- Win7 x64安装Paramiko出问题
今天上午windows下配置paramiko环境时出现问题,随手记录下来. 先说一下我的环境: win7 x64 旗舰版.Python3.5.0.pip8.1.0 pip install para ...
- 【转】Visual Stdio VS 错误 error : 0xC00000FD: Stack overflow. 更改堆栈空间解决栈溢出问题
原文见:http://www.cnblogs.com/xiangwengao/archive/2012/03/16/2399888.html 问题 给一个程序添加小功能,在debug下能正常运行,在r ...
- linux环境ubuntu: pushd: not found
编译错误: /bin/sh: 1: pushd: not found的问题 http://www.cnblogs.com/wansui/p/4230869.html 查看原因:进入/bin目录,查看s ...
- 数据库基础学习3-T-SQL语句
一.语句操作的基本方法 1.选中执行. 2.注释的方法‘--’. 二.数据类型 整数:int,bigint,smallint 小数:float,decimal(长度,精度) 字符:char(n),va ...
- 在win8.1上用Bitvise SSH Server 6.24(原名winsshd)搭建SSH2服务器
注意:此SSH是指运维领域的 SSH,不是指JavaWeb框架中的SSH. 运维领域:SSH=Secure Shell安全外壳协议 JavaWeb框架:SSH=Spring+Struts+Hibern ...
- WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇]
原文:WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇] 在[第2篇]中,我们深入剖析了单调(PerCall)模式下WCF对服务实例生命周期的控制,现在我们来 ...
- mysql 结合keepalived测试
vip:192.168.32.66 192.168.32.6 主库: mysql> show variables like '%read_only%'; +------------------+ ...
- hdu1334-Perfect Cubes
http://acm.hdu.edu.cn/showproblem.php?pid=1334 题意;求200以内所有满足a^ 3 == b^ 3 + c ^ 3 +d ^ 3 #include< ...