Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals again

 
Praveena khandavalli
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic