my dog learned polymorphism
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

No output classes

Andrea D'Angelo

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();"Aiko";"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

Joined: Oct 02, 2003
Posts: 11955

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 (
return true;
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

Joined: Oct 27, 2005
Posts: 20273

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.

How To Ask Questions How To Answer Questions
Andrea D'Angelo

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");


-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
To read more here is a good site:

John Eipe
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

Joined: Sep 25, 2010
Posts: 3
Thanks a lot to everybody! :-)
I agree. Here's the link:
subject: No output classes
It's not a secret anymore!