In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces which of the Java clone () and new is more efficient, the article is very detailed, has a certain reference value, interested friends must read it!
Several methods for object creation:
Use the new keyword
Use the clone method
Reflection mechanism
Deserialization
All four of the above can produce java objects.
The constructor is explicitly and explicitly called by 1BI 3.
2 is a photocopy of an existing object in memory, so the constructor is not called.
4 objects that restore the class from the file will not call the constructor
What is clone ()?
The copy object returns a new object, not the reference address of an object
The copy object already contains the information of the original object, rather than the initial information of the object, that is, each copy action is not aimed at the creation of an entirely new object.
Which is faster, clone () or new?
Using clone, copying data blocks in memory and copying existing objects is also a way to generate objects. The premise is that the class implements the Cloneable interface, and the Cloneable interface has no methods and is an empty interface, or such an interface can be called a flag interface. Only when the interface is implemented will the clone operation be supported. Some people may ask that all objects in java have a default parent class, Object.
There is a clone method in Object, why do you still have to implement the Cloneable interface? this is the meaning of the cloneable interface. Only when you implement this interface can you copy the object, because when jvm copies the object, it will check whether the object's class implements the Cloneable interface. If it does not, it will report a CloneNotSupportedException exception. Such interfaces also include Serializable interface, RandomAccess interface and so on.
It is also worth mentioning that the constructor is not called when the clone operation is performed. There are also the problems of deep copy and shallow copy in clone operation. With regard to this issue, there is a lot of relevant knowledge on the Internet, so I will not repeat it any more. Since it is not necessary to call the constructor to get the object through the copy operation, it is just a copy of the data block in memory, so the efficiency of copying the object is not necessarily faster than that of new.
Answer: no. Obviously, jvm developers also realize that generating objects through new accounts for the vast majority of developer-generated objects, so they optimize the use of new operations to generate objects.
For example:
Package com.miivii.javalib;public class Bean implements Cloneable {private String name; public Bean (String name) {this.name = name;} @ Override protected Bean clone () throws CloneNotSupportedException {return (Bean) super.clone ();}} package com.miivii.javalib;public class TestClass {private static final int COUNT = 10000 * 1000; public static void main (String [] args) throws CloneNotSupportedException {long S1 = System.currentTimeMillis () For (int I = 0; I
< COUNT; i++) { Bean bean = new Bean("ylWang"); } long s2 = System.currentTimeMillis(); Bean bean = new Bean("ylWang"); for (int i = 0; i < COUNT; i++) { Bean b = bean.clone(); } long s3 = System.currentTimeMillis(); System.out.println("new = " + (s2 - s1)); System.out.println("clone = " + (s3 - s2)); }} 打印结果:Is it true that new beat clone completely?
Let's do some simple things in the constructor, such as string interception. Just modify the Bean and look at the print when everything else remains the same
Package com.miivii.javalib;public class Bean implements Cloneable {private String name; private String firstSign;// gets the initials public Bean (String name) {this.name = name; if (name.length ()! = 0) {firstSign = name.substring (0,1); firstSign + = "abc" @ Override protected Bean clone () throws CloneNotSupportedException {return (Bean) super.clone ();}}
Conclusion: lightweight objects can use new, while other objects can use clone.
The above is all the content of the article "which is the more efficient clone () or new in Java? thank you for reading!" Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.