1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println("Objects have the same values"); 22: else 23: System.out.println("Objects have different values"); 24: } 25: }
A) Compilation error at line 20. equals() method was not defined. B) Compiles fine, runtime exception at line 20. C) Prints "Objects have the same values". D) Prints "Objects have different values".
MyClass inherits the default implementation of equals() in the java.lang.Object class.
In the Object class, equals() is the same as the == operator. This is the narrowest possible definition for object reference equality, so the idea in any subclass is to override equals() if you want a less strict definition of equality.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
agrah upadhyay
Ranch Hand
Joined: Sep 01, 2005
Posts: 579
posted
0
1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println("Objects have the same values"); 22: else 23: System.out.println("Objects have different values"); 24: } 25: }
A) Compilation error at line 20. equals() method was not defined. B) Compiles fine, runtime exception at line 20. C) Prints "Objects have the same values". D) Prints "Objects have different values".
The Answer is D . Please explain me why clearly
###############################################333 The Question is Related 2 Very Fundamentals Of Java. In Case Of Object References ,== operator returns true only when Both References 'r Refferring 2 Same Object.equals() Works Only On Objects And In Its Default Implementation, Gives The Same Result as By == Operator.For Class As String ,It Has Been Overriden So It Equates That's Content.But Here ,equals() Has Not Been Overridden So It Is Comparing Whether Both references Stand For Sme Object.
### Agrah Upadhyay 3rd Year B.Tech SASTRA Deemed University,Tamilnadu
<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
anand phulwani
Ranch Hand
Joined: Sep 10, 2005
Posts: 242
posted
0
What you have done is created 2 objects and compared those objects,and we all know that The equals method of object class works the same as ==,so as the refernce is diferrent these both are unequal,
its the same as doing this
which compiles and prints
Objects have different values
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%