简体   繁体   中英

Cloning mutable reference in immutable object will create unnecessary object every time it is referred

I thought to clarify the following issue:

If I have a mutable reference in an immutable object and I am cloning the mutable reference before passing it to any caller object, will this not create many copies of the mutable referenced object in the JVM?

Is this suggested, or do we have any other practice to get away with this?

Indeed this will create multiple copies/clones of the original mutable object, but all of those copies will be useful if other threads are using those. Otherwise, you can simply use the same copy within the same thread if not modifying of keep discarding the old ones if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM