• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why different output ???

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




i believe Strings are implemented as objects , then
why do i not get "Tandoori" as output...
 
Marcus Pant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11945
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic