What will the following code;s execution display?

class V {
    String s = null;
    V(String str){
        this.s = str;
    }
}

public class MyTest {
    public static void main(String[] args) {
        V v1 = new V("abc");
        V v2 = new V("abc");
        
        System.out.println((v1 == v2) + " " + v1.equals(v2));
    }
}
Explanation
By default an equals() method compares references just like a == method does. For "correct" objects' comparison it should be overriden.

@Override
public boolean equals(Object obj) {
    return s.equals(((V)obj).s);
}

Follow CodeGalaxy

Mobile Beta

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