• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

No output classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,
my name is Andrea and I am studying to get the SCJA. While solving some tests I met the following class:

class Dog {
String name;
public static void main(String[] args) {
Dog d1 = new Dog();
Dog d2 = new Dog();
d1.name="Aiko";
d2.name="Aiko";
if (d1 == d2) System.out.print("==");
if (d1.equals(d2)) System.out.print("dot=");
}
}

Could anyone,please, explain me why no output is produced running the Dog class? And, please, could you tell me some source where this behaviour is explained?
Thanks a lot!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get no output because the 'if' conditions are never true.

'==' compares if the references point to the same object - i.e. the same physical spot in memory. Since your two Dog objects were both created with the 'new' operator, they are indeed different, hence that one is false.

'equals' is a method. By default, when you define a new class, you inherit the parent class' version of 'equals'. the parent class of your Dog class is Object, and Object's 'equals' method is defined as...comparing the memory addresses.

If you want your Dog class to have a meaningful equals method, you would need to override the definition with something like

public boolean equals (Dog d)
{
if (this.name.equals(d.name))
return true;
else
return false
}



Since name is a String, and the String class has the equals method overridden to something useful, it should work. Note -that is untested code, written before I really woke up this morning. it may need some tweaking.

Also, there are some additional subtleties you will need to learn at some point, but probably not right now.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred Fred Fred... you know that should be "public boolean equals(Object o). The first thing should be a check if the object is of the right type, only after that can you cast to Dog and do a name-equality check.
 
Andrea D'Angelo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Fred. Your explaination was very useful.
Lets see if I understood good: lets consider the following class

class Cat {
public static void main(String[] args) {
Cat c1 = new Cat();
Cat c3 = new Cat();
Cat c2 = c1;
Cat c4 = c3;
if(c1==c2) System.out.print("c1==c2");
if(c1==c3) System.out.print("c1==c3");
}
}


here

-c1==c3 will never evaluate to true cause they have different memory locations;
-c2==c1 will be evaluate to true cause c2 has been initialized "to the same memory location" as c1.

Are my suppositions correct?
Thanks a lot again!
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup,
To read more here is a good site:
http://bytes.com/topic/java/insights/723476-overriding-equals-hashcode-methods

Thanks,
John Eipe
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get if (d1.equals(d2)) worked,

public boolean equals(Object o){ }

should be overridden in Dog class so that it will be compared meaningfully... and also public int hashcode() {}

By default, equals() in Object class will be called which will do internally reference equality check...
 
Andrea D'Angelo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to everybody! :-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic