What will be the result of compilation and execution of the following code?

public class Test {
    public static void main(String[] args) {
        TestClone t1 = new TestClone();
        TestClone t2 = (TestClone) t1.clone();
        for(int i = 0; i < t2.arr.length; i++) {
            t2.arr[i] = (t2.arr.length - i);
        }
        System.out.println(t1.arr.equals(t2.arr));
    }
}

class TestClone implements Cloneable {
    int arr[] = {1, 2, 3, 4, 5};
    public Object clone() {
        try {
            return super.clone();
        } catch(CloneNotSupportedException e) {
            System.out.println("Clone not supported");
            return this;
        }
    }
}
Explanation
The default implementation of clone() is used, therefore every reference variable of the new object will point to the same object as the variable of the original object.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты