If I am creating two objects for a same class then both the objects are not similar why?i,e A obj = new A(); A obj1 = new A();obj.equals(obj1) returns false why?

Questions by srisu77

Showing Answers 1 - 8 of 8 Answers

rahul

  • Apr 28th, 2006
 

RE: If I am creating two objects for a same class then...

Since every time  u say "new A()" a different object is created  and a diiferent memory is allocated to the instance of that class  so  obj and obj1 points to different memory location so obj.equals(obj1) will return false

  Was this answer useful?  Yes

dos

  • Jun 8th, 2006
 

i disagree, try this and it will return "EQ".i think you meant using a == b and this one would return "NOT". String a = new String("x"); String b = new String("x"); if (a.equals(b)) { System.out.print("EQ"); } else { System.out.print("NOT"); }

  Was this answer useful?  Yes

romal

  • Jun 15th, 2006
 

I aggre bcoz '==' sign is return true while both object r same.

and 'equal' is different bcoz it check only the equls string means same character base.

  Was this answer useful?  Yes

Sairam

  • Oct 27th, 2006
 

For the above example it cannot return false it has to return true

i.e if equals then returns true if uses == then returns false.

  Was this answer useful?  Yes

Parthasarathi.M

  • Oct 27th, 2006
 

In JDK5.0 equals() and == both are returns false.please try this example in JDK5.0public class EqualClass { /** Creates a new instance of EqualClass */ public EqualClass() { } public static void main(String args[]){ EqualClass eq1 = new EqualClass(); EqualClass eq2 = new EqualClass(); if(eq1.equals(eq2)){ System.out.println(" .equals() Equals"); }else if(eq1 == eq2){ System.out.println("== Equals"); }else{ System.out.println("Not Equals"); } }}

  Was this answer useful?  Yes

Equals method return on the basis of hashcode and if you do not override hashcode and equals
method the by default hashcode return int value of object physically address in memory so as when you compare two object by equals method it check for hashode which return two diff int value as both object have diff memory locations
hence return false.

  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