GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 537 of 937    Print  
Difference between == and equals()

  
Total Answers and Comments: 8 Last Update: May 27, 2008     Asked by: divya 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: jaiho123
 

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");
}

}



Above answer was rated as good by the following members:
sanjay637
January 30, 2006 02:12:49   
Ashok babu        

RE: Difference between == and equals()
if used for the compare the objcets where as the equals() is used to compare the content of the object.
 
Is this answer useful? Yes | No
January 30, 2006 03:13:23   
chandrashekar        

RE: Difference between == and equals()

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


 
Is this answer useful? Yes | No
January 30, 2006 03:22:10   
chandra        

RE: Difference between == and equals()

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

equals method checks wheather the strings are same or not


 
Is this answer useful? Yes | No
January 30, 2006 05:55:37   
sbarik Member Since: January 2006   Contribution: 27    

RE: Difference between == and equals()

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...


 
Is this answer useful? Yes | No
February 07, 2006 02:47:11   
vmshenoy Member Since: October 2005   Contribution: 508    

RE: Difference between == and equals()
equals is a function in java which is used to compare strings.i.e when we want to compare names etc then we should use this.When we want to compare integer values then we can do it by .
 
Is this answer useful? Yes | No
May 02, 2008 13:24:25   
vanigeetha Member Since: January 2008   Contribution: 4    

RE: Difference between == and equals()
( ) is used to compare two numbers (integers float double) but
.equals() is used to compare two strings.

 
Is this answer useful? Yes | No
May 13, 2008 03:16:48   
jaiho123 Member Since: May 2008   Contribution: 2    

RE: difference between equals() and ==

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");
}

}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 27, 2008 06:05:36   
sanjay637 Member Since: May 2008   Contribution: 2    

RE: Difference between == and equals()
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

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape