What is the difference between equals and ==?

Showing Answers 1 - 21 of 21 Answers

naveen

  • Apr 28th, 2006
 

Hi,

The difference b/w .equals and == is the comparision b/w object when using .equals and when using == its comparision of values b/w two.

Thanks

Naveen.

  Was this answer useful?  Yes

besically equal function use to check equality between objects

while == use check whether values are same

like obj1.equals(obj2)
while  abc = xyz are values

  Was this answer useful?  Yes

Anurag Chaturvedi

  • Apr 28th, 2006
 

== compares reference addresses of both objects.equals compares valus of objects.String s1="Anurag"String s2 = new String("Anurag")s1==s2 (it will come false as both have different addresses) s1.equlas(s2) would be true.

  Was this answer useful?  Yes

== compare : the object address. where as equals : compare the value of object.but exceptional case is string.but in case of String object suppose.String s1 = "nirav";String s2 = "nirav"; if(s1.equals(s2)) it returns "true"; and s1 == s2 also retrun "true";where as String s1 = new String("nirav");String s2 = new String("nirav"); second condition : if (s1.equals(s2)) it return "true" but s1 == s2 return "false".the difrence between one and two is :according to first codition java automatically refer the same address, address that before used to assing some value. (java use pretend word for first codition)while in second condition : java enforce to create a new object (using new).thats diffrence that you have to notice.

  Was this answer useful?  Yes

shiva

  • Apr 30th, 2006
 

i. "==" is used to check the equality between two primitive values and "equals" is for two objects.ii. "==" compares the addresses of two data but in contrast "equals" compares the value that is being referenced by objects.In the case of string, if String s1="shiva" and String s2="shiva" == and equals behaves in the same because Java treats s1 and s2 as primitive data here. but if String s1= new String("shiva") and String s2= new String("shiva") == and equals behaves differently because Java treats s1 and s2 as objects.

  Was this answer useful?  Yes

Swapna

  • May 2nd, 2006
 

.equals compares the values of the two objects whereas == check for the address references to be same

  Was this answer useful?  Yes

Swap

  • May 2nd, 2006
 

.equals checks for the values to be same whereas == checks for the refereces to be same.

  Was this answer useful?  Yes

shivalak

  • May 3rd, 2006
 

i. "==" is used to check the equality between two primitive values and "equals" is for two objects.ii. "==" compares the addresses of two primitive data or object but in contrast "equals" compares the value that is being referenced by objects. In the case of string, 1. if String s1="shiva" and String s2="shiva" == and equals behaves in the same because Java treats s1 and s2 as primitive type. 2. if String s1= new String("shiva") and String s2= new String("shiva") == and equals behaves differently because Java treats s1 and s2 as object type.

  Was this answer useful?  Yes

pooja saxena

  • May 22nd, 2006
 

== is a comparision "operator" fopr comparing any two values but .equals is a String class "Function" for performig comparision between two strings

  Was this answer useful?  Yes

The operator '==' checks whether or not two variables ( both primitive like 'int' variable and reference variables like 'String' variable) contain same data. In case of objects, reference variables contain the address of the object in the memory. Hence, '==' compares addresses of the two objects which are held by reference variables.

.equals() method checks the members of the objects pointed by the two reference variables whether they are same or not.

Hence, we have to use .equals() method instead of ?==? operator for comparing the content of two objects and we have to use '==' operator to compare primitive types like 'int' , 'float', 'chat', 'boolean', 'byte', etc. 

Seetha Ram Janapala  -- sitaram81581@rediffmail.com

  Was this answer useful?  Yes

1. If we give == operator between two reference variables,It compares whether two reference variables refer to the same object.
2. If we give equals?between two reference variables,It compares whether two objects are same.

  Was this answer useful?  Yes

sampra

  • Feb 22nd, 2008
 

The difference b/w .equals and == is the comparision b/w object when using .equals and when using == its comparision of values b/w two.
but some time it compare content some time address in the case of string it compare content

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions