Difference between == and equals()

Showing Answers 1 - 14 of 14 Answers

Ashok babu

  • Jan 30th, 2006
 

"==" if used for the compare the objcets where as the "equals()" is used to compare the content of the object.

  Was this answer useful?  Yes

chandrashekar

  • Jan 30th, 2006
 

Hi,

 == operator is the equality operator

equals() is for content comparison

 == operator checks if both object references refer to the same memory location eg: if (a==b) it means it verifies whether both a and b refer to the same location in memory.

equals() if for content comparison.

eg:String a ="chandra"; String b="chandra"; String c ="radha";

if ( a.equals(b)) returns true bcoz both contains the same content.

if (a.equals(c)) returns false bcoz a and c contents are different.

 I hope this would have solved your doubt.

Thanks,

Chandra

  Was this answer useful?  Yes

chandra

  • Jan 30th, 2006
 

== checks wheather two strings are pointing to same location or not.

equals method checks wheather the strings are same or not

  Was this answer useful?  Yes

sbarik

  • Jan 30th, 2006
 

The == operator  checks identity of
objects (that is, in a check a == b, it checks whether the two variables a
and b refer to the same object).

Wheres as .equals() compares the values...

eg-->

a="suvendu";b=a; then a==b will be true;

a="suvendu",b=new string(a); Then a==b will be false...

  Was this answer useful?  Yes

jaiho123

  • May 13th, 2008
 

public class Test1

{


public static void main(String args[])

{


String s1="ashu";

String s2="ashu";


if(s1==s2)                            // same Object refrence

System.out.println("H!!  first time");  


String s3 = new String("ashu");

String s4 = new String("ashu");

if(s3==s4)                              // different Objects


System.out.println("H!! Second time");
 
if(s3.equals(s4))             // this will return true as it wil check object contents


System.out.println("H!! Third Time");
}

}

sanjay637

  • May 27th, 2008
 

the operator == compares the content of two strings
equals method compares the references of two variables

generally we use == operator for comparing numbers

thanks
sanjay bandi

  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