Originally posted by rgowka1:
equal() does a deep comparison. ie it compares the contents.
== does a shallow comparision i.e it just test to see if the both the operands have a same reference.
While creating strings (say u want s="hello" ), in java strings are immutable, so the VM searches to see if there is a "hello" string already. if there is one, it assigns that address to the variable s.
now while comparison say u want to compare s with a(some other varible), then s.equal(a) compares the actual contents where as s==a just compares the reference.
Careful, == compares the contents of the variable or reference. In the case of a refernce, this is the memory location of an object so, it tests to see if the references refer to the same object.
equals() is a method defined in Object which basically does the same thing, checks to see if the refernces refer to the same object. However, in some of the APIs including String class, equals is overridden to compare the contents of the reference and not just to see if it is the same object. You can in any of your classes override equals to do what is appropriate for your class.
So, check the documentations for any class (or super class) that you are using from the API's to see what the effect equals() will have.