开发者

what is the general rule for determining without testing if Java methods deep copy?

Here is one (typical?) example taken from the List.addAll doc that does not seem to say whether the additions to the target list will be deep copies. Other than testing, what is the general method of quickly parsing documentation or general understanding that allows a determination if standard Java classes and methods deep copy? Oracle writeup on List.addAll

addAll

boolean addAll(Collection<? extends E> c)

    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

    Specified by:
        addAll in interface Collection<E>

    Parameters:
        c - collection containing elements to be added to this list 
    Returns:
        true if this list changed as a result of the call 
    Throws:
        UnsupportedOperationException - if the addAll operation is not supported by this list 
        ClassCastException - if the class of an element of the specified collection prevents it from being added to this list 
        NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null 
        IllegalArgumentException - if开发者_如何学JAVA some property of an element of the specified collection prevents it from being added to this list
    See Also:
        add(Object)


Unless something specifies that it takes a deep copy, I'd always assume it doesn't. It's very rare that deep copies are taken, particularly as it relies on the objects themselves supporting cloning.


There are no methods on the Java standard API that do a deep copy. Deep copies generally only happen implicitly through serialization (possibly also cloning, but I've never seen this), and some frameworks/libraries may offer this explicitly through a utility method, but it's a rare exception and not even possible to do for arbitrary Objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜