aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why different output ???" Watch "Why different output ???" New topic
Author

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
Greenhorn

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: 11479
    
  94

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
Greenhorn

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)
)
System.out.println("Tandoori");
else
System.out.println("Mutton");
}
}

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
 
 
subject: Why different output ???