What is the differance between equals() method and ==

Showing Answers 1 - 13 of 13 Answers

The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value.The .equals() method to compare strings for equality(content).

  Was this answer useful?  Yes

Guest

  • Dec 25th, 2005
 

the equals method just comapares the characters inside the String object while the == symbols referes to see whether the two objects refer to the same instance.

for example

import java.io.*;
public class equals1
{
public static void main(String args[])
{
String a ="hello";
String b ="hello" ;
String c = new String(a);
String d = new String(a);
System.out.println("a equals b" + a.equals(b));
System.out.println("a ==     b" + (a == b));
System.out.println("a ==  c" + (a == c));
System.out.println("c == d"  + (c == d));

}
}

jitender sharma

  • Jan 8th, 2006
 

normally == operator is used to compaire the integer value.where .equals( ) is used to compaire the two strings.

  Was this answer useful?  Yes

equals method compares the chars inside the string object.the == operator compares 2 object references to see whether they refer to same instance.

  Was this answer useful?  Yes

dilipiyer

  • Jan 20th, 2006
 

== checks if the object is of the same instance whereas .equals() compares the character content.

Eg : String s1 = "hello";
     String s2 = "hello";

Here s1==s2 is false and s1.equals(s2) is true

  Was this answer useful?  Yes

suresh

  • Jan 25th, 2006
 

equls() compares characters inside the string where as (== ) compares object references of the string

example

import java.lang.String.*;
class Equals
{
public static void main(String args[])
{
String a ="hello";
String b ="hello" ;
String c = new String(a);
String d = new String(a);
System.out.println("a equals b" + a.equals(b));
System.out.println("a ==     b" + (a == b));

if(c==d)

System.out.println("it is equal");
else
System.out.println("not equal");
 
}
}

  Was this answer useful?  Yes

1.) The == operator compares the instance of the two objects,and returns true if the instance is the same, i.e they are refering to the same object. E.g String s1,s2;s1="Hello";s2="Hello";System.out.println(s1==s2);now we trying to save something which is already in the memory,so no new instance is created while creating s2, and s2 gets the instance of the first string, this done java automatically to save memory.so (s1==s2) in this case will return a true value.but if new operator is used to create the string object every object creates a new instanceso if String s1=new String ("Hello");String s2=new String ("Hello");System.out.println(s1==s2);in this case(s1==s2) will return a false value as they are having different instances now.so be very careful while using == operator on string, better use .equals operator which compares two object for equality.

  Was this answer useful?  Yes

All objects have both identity (the object's location in memory) and state (the object's data). The == operator always compares identity. The default implementation of equals compares identity as well. Sometimes the default implementation of equals has the desired behaviour (as in a type-safe enumeration, for example), but equals should usually compare state, not identity.  String class overrides the equals method to compare the actual content. Should your requirement demand comparing the content of the object, override the equals method. And if u override equals dont forget to override hashCode() since , if u say two objects are equal, then their hashCodes should be equal.

  Was this answer useful?  Yes

(==) compares object refernces where as .equals compares variables

for example take the following program

class Suresh
{
 public static void main(String args[])
 {
 String str=new String("hello");
 String str1="hello";
 if(str==str1)
  System.out.println("same");
 else
  System.out.println("not same");

 if(str.equals(str1))
  System.out.println("same");
 else
  System.out.println("not same");

}
}

  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

Balrog

  • Jan 31st, 2007
 

Your'e WRONG!!
String s1 = "hello";
String s2 = "hello";
is s1 === s2? TRUE
This is because SAME literals have the same reference in memory!!!
Don't post incorrect response.

  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