File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No output classes

 
Andrea D'Angelo
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
John Eipe
Ranch Hand
Posts: 215
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to everybody! :-)
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic