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