sparsh khandelwal wrote:
Hi Sparsh,
In
Java there are two different ways to check for equality.
1. == operator
2. equals() method (a part of Object which is a parent class to every other class)
While equals() works with object types only == works with primitives as well.
equals() checks the equality using the value contained by two objects (you can also do that with the same object though)
== gives a true result only when the two primitives have the same value (downcasting and upcasting should be considered also) or two references point to the exact same object.
you can override the equals() in your class and give meaning to it as to how you want your class objects to be checked for equality.
Also, in your code s1.concat("a") won't do any change to s1 itself which is why at line 7 the result of equals() is true.
Strings are immutable objects and whenever you operate on a String object it gives you another String object.
Phew ... this much for now ...
cheers :-)