What is the difference between a.Equals(b) and a == b?
Answer Posted / rajan
equals() method compares the state of an object i.e. it
compares the contents of the two objects.
but == compares the instance of the object i.e. it comparing
the identifier(references).
Ex:
public class Test2
{
public static void main(String args[])
{
// Create two equal but distinct strings
String a = new String(new char[]
{'h', 'e', 'l', 'l', 'o'});
String b = new String(new char[]
{'h', 'e', 'l', 'l', 'o'});
System.out.println(a==b);
System.out.println(a.equals(b));
// Now let's see what happens with the same tests
// with variables of type object
Object c = a;
Object d = b;
System.out.println(c==d);
System.out.println(c.equals(d));
}
}
Output is:
false
true
false
true
From the above example the String object is creating new
instances, so == compares the instances are equal are not.
equal() method compares the contents of a two object .
| Is This Answer Correct ? | 8 Yes | 4 No |
Post New Answer View All Answers
Which method is used to force all the validation controls to run?
What is the difference between client-side and server-side validations in webpages?
What is the difference between dynamic SGA and static SGA?
What are session state modes in asp.net?
Is session stored in browser?
How can I configure asp.net applications that are running on a remote machine?
Can we have multiple master pages in asp net?
What is difference between session and cookies?
What are the advantages of asp.net?
In which event are the controls fully loaded?
How is mvc different from asp.net? : Asp.Net MVC
What is a pixel url?
List all templates of the repeater control.
What is state management in .net?
What are the different properties of server control that exists?