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


Please Help Members By Posting Answers For Below Questions

Explain Life cycle of ASP.NET page when a request is made.

747


What are strong names?

579


How to create discussion forum in asp.net mvc? : Asp.Net MVC

529


Can you explain one critical mapping? Performance issue which one is better? Whether connected lookup tranformation or unconnected one?

805


Explain the code Access Security (CAS) in .net Framework?

664






What is sta?

553


What are the new navigation controls in asp.net 2.0?

536


What is the difference between page.registerclientscriptblock and page.registerstartupscript?

480


What is asp.net architecture?

527


What is the web.config file in asp?

634


What is the current version of asp.net?

493


Define repository pattern in mvc.net? : asp.net mvc

511


What are the uses of list view control in Asp.net?

611


What do you mean by authorization?

518


What is the use of autowireup in asp.net?

556