• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

equals again

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

 
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!