java json clone_java Object对象的clone方法

参考copy链接:http://blog.csdn.net/bigconvience/article/details/25025561

在看原型模式,发现要用到clone这个方法,以前和朋友聊过,没怎么看过,刚好要用,就看看了。

源码解释:

/*** 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 {@codex}, the expression:

*

*

 
  

* x.clone() != x

* will be true, and that the expression:

*

*

 
  

* x.clone().getClass() == x.getClass()

* will be {@codetrue}, but these are not absolute requirements.

* While it is typically the case that:

*

*

 
  

* x.clone().equals(x)

* will be {@codetrue}, this is not an absolute requirement.

*

* By convention, the returned object should be obtained by calling

* {@codesuper.clone}. If a class and all of its superclasses (except

* {@codeObject}) obey this convention, it will be the case that

* {@codex.clone().getClass() == x.getClass()}.

*

* 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 {@codesuper.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 {@codesuper.clone}

* need to be modified.

*

* The method {@codeclone} for class {@codeObject} performs a

* specific cloning operation. First, if the class of this object does

* not implement the interface {@codeCloneable}, then a

* {@codeCloneNotSupportedException} is thrown. Note that all arrays

* are considered to implement the interface {@codeCloneable} and that

* the return type of the {@codeclone} method of an array type {@codeT[]}

* is {&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSON是基于JAVA7编写,对比阿里的JSON三次测试结果如下: 10万次序列化,1万次反序列化,毫秒。 阿里序列化时间 1229 1133 1179 阿里反序列化时间 478 523 466 HZS序列化时间 1089 998 1010 HZS反序列化时间 606 623 635 测试代码如下: { org.hzs.json.JSONObject bjson; java.util.LinkedList<String> jd_Set = new java.util.LinkedList<>(); java.util.Random d1 = new java.util.Random(); java.util.UUID d2; int ji_i; long ji起始时间_i; long ji截至时间_i; java.util.Date date = new java.util.Date(); //生成1万个序列化後的文本 for (ji_i = 0; ji_i < 10000; ji_i++) { bjson = org.hzs.json.JSONObject.d副本(); bjson.put("a1", d1.nextDouble()); bjson.put("a2", d1.nextDouble()); bjson.put("a3", d1.nextDouble()); bjson.put("a4", d1.nextInt()); bjson.put("a5", d1.nextInt()); bjson.put("a6", d1.nextLong()); bjson.put("a7", d1.nextBoolean()); d2 = java.util.UUID.randomUUID(); bjson.put("b1", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b2", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b3", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b4", d2.toString()); bjson.put("c", new java.util.Date()); jd_Set.add(bjson.toString()); } com.alibaba.fastjson.JSONObject ajson, a1json = new com.alibaba.fastjson.JSONObject(); ji起始时间_i = java.util.Calendar.getInstance().getTimeInMillis(); for (ji_i = 0; ji_i < 100000; ji_i++) { ajson = (com.alibaba.fastjson.JSONObject) a1json.clone(); ajson.put("a1", d1.nextDouble()); ajson.put("a2", d1.nextDouble()); ajson.put("a3", d1.nextDouble()); ajson.put("a4", d1.nextInt()); ajson.put("a5", d1.nextInt()); ajson.put("a6", d1.nextLong()); ajson.put("a7", d1.nextBoolean()); d2 = java.util.UUID.randomUUID(); ajson.put("b1", d2.toString()); d2 = java.util.UUID.randomUUID(); ajson.put("b2", d2.toString()); d2 = java.util.UUID.randomUUID(); ajson.put("b3", d2.toString()); d2 = java.util.UUID.randomUUID(); ajson.put("b4", d2.toString()); ajson.put("c", new java.util.Date()); ajson.toString(); } ji截至时间_i = java.util.Calendar.getInstance().getTimeInMillis(); System.out.print("阿里变量序列化时间:"); System.out.println(ji截至时间_i - ji起始时间_i); ji起始时间_i = java.util.Calendar.getInstance().getTimeInMillis(); for (ji_i = 0; ji_i < 10000; ji_i++) { ajson = com.alibaba.fastjson.JSONObject.parseObject(jd_Set.get(ji_i)); } ji截至时间_i = java.util.Calendar.getInstance().getTimeInMillis(); System.out.print("阿里反序列化时间:"); System.out.println(ji截至时间_i - ji起始时间_i); ji起始时间_i = java.util.Calendar.getInstance().getTimeInMillis(); for (ji_i = 0; ji_i < 100000; ji_i++) { bjson = org.hzs.json.JSONObject.d副本(); bjson.put("a1", d1.nextDouble()); bjson.put("a2", d1.nextDouble()); bjson.put("a3", d1.nextDouble()); bjson.put("a4", d1.nextInt()); bjson.put("a5", d1.nextInt()); bjson.put("a6", d1.nextLong()); bjson.put("a7", d1.nextBoolean()); d2 = java.util.UUID.randomUUID(); bjson.put("b1", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b2", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b3", d2.toString()); d2 = java.util.UUID.randomUUID(); bjson.put("b4", d2.toString()); bjson.put("c", new java.util.Date()); bjson.toString(); } ji截至时间_i = java.util.Calendar.getInstance().getTimeInMillis(); System.out.print("HZS变量序列化时间:"); System.out.println(ji截至时间_i - ji起始时间_i); ji起始时间_i = java.util.Calendar.getInstance().getTimeInMillis(); for (ji_i = 0; ji_i < 10000; ji_i++) { bjson = org.hzs.json.JSONObject.d副本(jd_Set.get(ji_i)); } ji截至时间_i = java.util.Calendar.getInstance().getTimeInMillis(); System.out.print("HZS反序列化时间:"); System.out.println(ji截至时间_i - ji起始时间_i); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值