Clone method for Java arrays

前端 未结 2 1899
自闭症患者
自闭症患者 2020-11-27 13:03

What exactly does the clone() method in Java return when used on an array? Does it return a new array with data copied from the original?

Ex:

int[] a         


        
2条回答
  •  萌比男神i
    2020-11-27 13:23

    clone() method 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 x, the expression:

     x.clone() != x
    

    Will be true, and that the expression:

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

    Will be true, but these are not absolute requirements.

    While it is typically the case that:

     x.clone().equals(x)
    

    will be true, this is not an absolute requirement.

    By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

提交回复
热议问题