aspose file tools*
The moose likes Beginning Java and the fly likes No output classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "No output classes" Watch "No output classes" New topic
Author

No output classes

Andrea D'Angelo
Greenhorn

Joined: Sep 25, 2010
Posts: 3
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

Joined: Oct 02, 2003
Posts: 11328
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andrea D'Angelo
Greenhorn

Joined: Sep 25, 2010
Posts: 3
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

Joined: May 23, 2008
Posts: 215
Yup,
To read more here is a good site:
http://bytes.com/topic/java/insights/723476-overriding-equals-hashcode-methods

Thanks,
John Eipe

www.cs-repository.info
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
Andrea D'Angelo
Greenhorn

Joined: Sep 25, 2010
Posts: 3
Thanks a lot to everybody! :-)
 
wood burning stoves
 
subject: No output classes