Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

equals()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Maha:
Satyavani, Please use the < pre > your code comes here < pre > to align your code. Also put the 'pre' tags without the space in bet < and >. Please edit your code to make it easy for others to read.

class MyClass
{
static int maxElements;

MyClass(int maxElements)
{
this.maxElements = maxElements;
}

}

public class Q19
{
public static void main(String[] args)
{

MyClass a = new MyClass(100);
MyClass b = new MyClass(100);

if(a.equals(b))
System.out.println("Objects have the samevalues");
else
System.out.println("Objects have different values");
}
}
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";

I ran the code and found to my surprise ans is the out put. But equals() compares the contents of the object, so both the objects contain 100 then why is the output D? can some one clear my doubt?

[This message has been edited by maha anna (edited April 17, 2000).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MyClass a = new MyClass(100);
MyClass b = new MyClass(100);
when ever u say new ,a new instance is created they are two different instances.
So try out this one
//MyClass b = new MyClass(100); //comment it in ur program
MyClass b;
b=a;
It gives "Objects have same values" as u r not creating a new instance.


------------------
Shashi
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every class will inherit equals(). But you have to override that inorder to get the required behaviour otherwise it will act exactly like == which compares memory addresses not the contents. In MyClass since you haven't ovveridden equals() it's acting like ==.
Hope your doubt is cleared.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satyavani,
I have nothing more to add. Both shashi kiran, sree said is correct. The default inherited equals(Object) method just compares if the 2 references refer to the SAME object It doesn't do anything more than that. Since you create 2 NEW objects they are in different places in heap and since your program didn't override the inherited equals(..) it says 'they both are NOT EQUAL.'
regds
maha anna
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Satyavani,
what Maha anna said is right.u have to override the default equals() method of the Object class. some of the classes that override equals() are String,File,BitSet,Boolean,all wrapper classes,Date and Calendar.
Madhuri.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody my doubt is cleared.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic