aspose file tools*
The moose likes Java in General and the fly likes an equals question from a mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "an equals question from a mock exam" Watch "an equals question from a mock exam" New topic
Author

an equals question from a mock exam

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
I just don't get it ( how come the equals doesn't find them equal?):

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: }


Prints: "Objects have different values"
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
When you don't override the equals method from Object, the only check that is made when you call .equals is if the two object references refer to the same object.
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
Doh!!

Thank you so much!!
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
Ah yes, is does the " == " operator on the reference values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an equals question from a mock exam
 
Similar Threads
equals()
.equals()
equivalence
equals method
Tell me the output?