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
Explain Life cycle of ASP.NET page when a request is made.
What are strong names?
How to create discussion forum in asp.net mvc? : Asp.Net MVC
Can you explain one critical mapping? Performance issue which one is better? Whether connected lookup tranformation or unconnected one?
Explain the code Access Security (CAS) in .net Framework?
What is sta?
What are the new navigation controls in asp.net 2.0?
What is the difference between page.registerclientscriptblock and page.registerstartupscript?
What is asp.net architecture?
What is the web.config file in asp?
What is the current version of asp.net?
Define repository pattern in mvc.net? : asp.net mvc
What are the uses of list view control in Asp.net?
What do you mean by authorization?
What is the use of autowireup in asp.net?