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
What ports must be open for DCOM over a firewall? What is the purpose of Port 135?
How you can add an event handler?
Which ASP.NET configuration options are supported in the ASP.NET implementation on the shared web hosting platform?
Differentiate between namespace and assembly.
Can we store object in viewstate?
What is comparevalidator?
List the types of authentication supported by asp.net?
How many web.config files can I have in an application?
How can we communicate with each server in N-tier Architecture? and what are the methods?
What is difference between asp.net and asp.net mvc? : Asp.Net MVC
What do you mean by authentication?
How do u deploy ur project?
What is the procedure to create the environment for asp.net? : asp.net mvc
What are directives in asp.net?
Why is the standalone environment only useful during the development process?