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

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        
        A a1 = new A();
        A a2 = (A) a1.clone();
        
        System.out.println(a1 == a2);
        System.out.println(a1.min == a2.min);
        System.out.println(a1.max == a2.max);
    }
}

class A implements Cloneable {
    Integer min = new Integer(100);
    Integer max = new Integer(1000);
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
Explanation
x != x.clone() is the first condition of clone() contract as described in documentation ( http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone() ) . Variables min and max are equal to the cloned versions because they hold references to the same objects: standart clone() creates a shallow copy of an object - it simply copies references to objects-members.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes