The moose likes Beginning Java and the fly likes Why different output ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why different output ???" Watch "Why different output ???" New topic

Why different output ???

Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66

i believe Strings are implemented as objects , then
why do i not get "Tandoori" as output...

Have A Nice Day !!!
Marcus Pant

Joined: May 21, 2004
Posts: 7
you have to overload boolean equals(Object obj) in Pizza to consider the comparison of the strings.
otherwise if( small.equals(large) ) calls Object.equals which just compares the addresses of the referenced objects.
in an more complex case, you also have to overload public int hashCode().

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Hi Prashanth,

The problem is that your Pizza class extends the java.lang.Object class, and as such, it inherits the equals() and the hash() methods from the Object class.

If you look at the API documentation for the equals method in the Object class, you will find that:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
(Highlighting is mine).

In other words, since you are using the default implementation of the equals() method, the statements small.equals(large) is the same as the statement small == large. This later statement is incorrect - you have created two separate Pizza objects.

If you want the statement small.equals(large) to work, you will have to override the equals() method. One possible way to do this could be:

Of course, if you override the equals() method, you should also override the hash() method. But I will leave that exercise to you .

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Saravanan Jothimani

Joined: Feb 25, 2005
Posts: 26
class Vijay
public static void main(String[] args)
Pizza small = new Pizza("Tandoori");
Pizza large = new Pizza("Tandoori");
if( small.equals(large)

class Pizza
String s;
Pizza(String s)
this.s = s;

Output: Mutton

In Pizza method you are getting String s as a parameter and String s as local variable , in this you are using this.s = s; inside pizza method. So the parameter s will be assigned to this.s, so parameter s will be assigned to the local s. Here in this local s does not contain any string, so it return null. When comparing with equals method it return false. So it is printing "Mutton"
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
Thank you guys
I agree. Here's the link:
subject: Why different output ???
It's not a secret anymore!