hi all , here is the prog and its output is Prints "Objects have different values"; can anybody explain why is it so...if i run the class by creating String objects rather than the MyClass object ..the result is 'equal'..i am confused please explain..thanx in advance Praveena -------------------------------------- 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: } ------------------------------------------
mark stone
Ranch Hand
Joined: Dec 18, 2001
Posts: 417
posted
0
because the equals method for class String is overridden. This overridden method compares for the content in the contained string. (this is different from the equals method for class Object) i hope it's clear now
Originally posted by Praveena khandavalli: hi all , here is the prog and its output is Prints "Objects have different values"; can anybody explain why is it so...if i run the class by creating String objects rather than the MyClass object ..the result is 'equal'..i am confused please explain..thanx in advance Praveena -------------------------------------- 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: } ------------------------------------------
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
Originally posted by Praveena khandavalli: hi all , here is the prog and its output is Prints "Objects have different values"; can anybody explain why is it so...if i run the class by creating String objects rather than the MyClass object ..the result is 'equal'..i am confused please explain..thanx in advance Praveena
MyClass extends Object (implicitly). Object's implementation of equals() compares the two objects and returns true only if they are the same object reference. This is basically the same behavior as using the equality operator (==). Since the two objects are different objects, Object.equals() will return false. IF you want to implement an equals method that works for this example, you have to write it yourself. In MyClass, I would add...
Of course, in YOUR specific example, since you're using a static variable, this test for equality will ALWAYS return true. I'm not sure why you want this variable to be static.
Rob
Rob
SCJP 1.4
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.