Vinod Vijay wrote:
why java have provided a seperate method "equal()" to compare if can do the same with "==" only.
Because they are not the same thing. The "==" operator is used detect whether two references are pointing the the exact same object. The equals() method is class specific -- for example, strings are equal when the value is equal, even if it is two different string objects.
what you have are references to the objects. You can think of them as like little cards that tell you the address of a house. using '==' tells you if both pieces of paper say "1600 Pensylvania avenue". using equals() might tell you if both homes are worth $150,000, or if both have three bedrooms, or if both are exactly the same square footage...
equals() can be defined to be whatever 'makes sense' for that object type.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors