• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to differntiate equals and == ?  RSS feed

 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried this code i am not able to understand why is it giving out "Not equal" when both the objects refer to same class.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try n.equals(n1) instead of n==n1 and see what happens.
To find out why, check out this thread http://www.coderanch.com/t/546585/java/java/difference-between-equals
 
Sandeep Kumar B
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the program



Read about .equals here
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

So, even if you initialize two objects with same constructor parameters, equals operator for objects would still be false unless we assign n=n1 or n1=n Or we override equals method(and hashCode ) as per our needs
For String, equals method is implemented to compare the contents of the string when equals operator is applied but for objects, its left to developer if he needs more functionality
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== asks if the value in the variable is the same. For object references, which is what n and n1 are, it (more or less) holds the memory location of the object. since you have two objects, which must by definition exist in different spots in memory, their addresses are different.

your n and n1 are like index cards that hold the street address of a house. When you say "n = new neww();", you write the address of the house you just build on the index card. when you say "n1 = new neww();", you build a second house with a different address. So, when you say "is the address of house one the same as the address of house 2?", clearly the answer is no.

Now, by default, the equals() method does exactly the above. it compares the address. Some classes (such as the String class), override that method. You can (and often should) do the same for any class you create. In the equals() method, you then define what it means to be 'logically equivalent'. for houses, you may say "if they both have the same number of bedrooms, bathrooms, and the total square footage is withing 5% of each other". For Strings, it is defined as 'the same sequence of characters'. For other classes, the developer has to decide what it means for them to be 'equal'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!