Question: nalyze the following code. Program 1 : public class Test { public static void main ( String [ ] args ) { Object a 1

nalyze the following code.
Program 1:
public class Test {
public static void main(String[] args){
Object a1= new A();
Object a2= new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(Object a){
return this.x ==((A)a).x;
}
}
Program 2:
public class Test {
public static void main(String[] args){
Object a1= new A();
Object a2= new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a){
return this.x == a.x;
}
}
Program 1 displays false and Program 2 displays false
Program 1 displays true and Program 2 displays false
Program 1 displays false and Program 2 displays true
Program 1 displays true and Program 2 displays true

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!