What is the difference between == & .equals

Showing Answers 1 - 38 of 38 Answers

Jitendra Desale

  • Jul 11th, 2005
 

== used for comparing two Strings Objects rather than value. 
 
.equels is used for comparing two strings

  Was this answer useful?  Yes

santoshini

  • Aug 18th, 2005
 

== is equals is used for checking the equality of values of all datatypes, 
.equals is used for checking the equaliy of strings.

  Was this answer useful?  Yes

D.S.Reddy

  • Sep 12th, 2005
 

== is used to check whether the references of the both are equal or not?

.equals() is used to the compare the values in that references.

  Was this answer useful?  Yes

Archana

  • Sep 15th, 2005
 

== is used to compare the number type data types viz. int, float,double  and also the boolean datatypes.

e.g. int x=10;

if(x==10)

{

// your code

}

boolean flag = true;

if(flag==false)

{

// Action1

}

if(flag==ture)

{

//Action2

}

.equals() method is used to compare the String data types

viz.

String str = "Hello";

if(str.equals("Hello"))

{

// Your code of action

}

  Was this answer useful?  Yes

Praveen Kumar

  • Sep 27th, 2005
 

1.    == is swallo comparison. Means it will use the memory location for comparison rather that the data.

2. .equals() is indepth comparison. I will check the content between two object rather that the memory location.

Vinay Kumar

  • Sep 27th, 2005
 

== is used for checking the datatypes values like int,float,double..

.equals is used to check the Objects like String,Vector ....

mehar

  • Oct 3rd, 2005
 

The difference between ==  and .equals is

String a="john",b="joi";

if (a.equals(b))// it compares internaly for the same materials

if(a==b)// it compares for the memory location

udays

  • May 20th, 2008
 

.equals -> Compares chracters between two strings are equal.
==        -> Compares  object reference between two strings are same .
 
--Khushi

  Was this answer useful?  Yes

== is used to compare the values of primitive typed variables and the memory location of Objects.
equals is used to compare the content of the two objects.

== is normally much faster than equals since it just checks the memory location of two objects.


The difference can be told in this simple way, for any object:


obj.clone() == obj
will always be wrong while


obj.clone().equals(obj)
will always be right.

equals() to method is used to compares only the values in a  String .
While == operator is used to compare values as well as hashcode or memory allocation. This is used to compare to objects 

  Was this answer useful?  Yes

sankar129

  • Sep 29th, 2008
 

== is used to check whether the two objects are referes to the same object
.equals() methos is used to check whether the two objects are meaning fully equal

  Was this answer useful?  Yes

CASE1:

       String s3 = "hi";
        String s="hi";
        String s2=s3;
       
        System.out.println("hash code S " +s.hashCode());
        System.out.println("hash code S2 " +s2.hashCode());
        System.out.println("hash code S3 " +s3.hashCode());
        System.out.println("double equal "+(s==s2));
        System.out.println("equal "+ s.equals(s2));

out put:
double equal true
equal true

JVM crate only one Object and pointing to three reffernce variable.
Since   refferences are pointing to same object propertie i.e s,s3,s2 pointing to "hi' .so == ill compare the refferences  locations,so here refferences locations are same ,so ill get true.
.equals compare the object properties ,here object properties same ,ill get true .

CASE 2:

     String s1 = new String("hi");
        String s2 ="hi";
        System.out.println("double equal " +(s1==s2));
        System.out.println("equal  " +s1.equals(s2));

out put is :

double equal false
equal  true

so == comapre the reffernces but .equals compare the object properties.

Thanks,
Malli.

  Was this answer useful?  Yes

laxminarsaiah

  • Dec 27th, 2011
 

Equals() method is expected to check for the equivalence the contents of the objects.

Whereas the == operator is expected to check the actual object instances are same or not.

Code
  1. s1 = new String("abc");

  2.  s2 = new String("abc");

  3.  

  4. if(s1.equals(s2))

  5.       System.out.println("s1.equals(s2) is TRUE");

  6.  else

  7.       System.out.println("s1.equals(s2) is FALSE");

  8.  

  9.  

  Was this answer useful?  Yes

JBUG

  • Jan 28th, 2012
 

The "==" operator can be used to test primitive values for equality, and can also be used to determine if two object references point to the same underlying object.

For Java objects, the equals method will return true if the argument is equal to the object on which the method is invoked, where equality is defined by the objects class implementation of the equals method.

  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