File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.equals() and "==" what is the output of the following code and why?

 
sharma chetan
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically, == checks if both refer to the same object. whereas equals checks if they have the same value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have edited your post by adding what is described on the link, and you can see how much better it looks.
Please supply more details. What do you think the output will be, and what was the output? Was there a difference? When we know that, we can help you sensibly.
 
Joe Ridener
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those methods are all inherited methods from class Object.
The first one hashcode() returns an integer value. The default integer return value will be the address location in memory alloted for that particular object. In the event you want to over ride it ( in such as using hashtables or similar data structures that use them for placing elements in empty cells) you would provide your own implementation. It is tricky because you have a reload factor and often the arithmetic used for this involves the MOD operator '%' for placing the object according to it's hashcode value.

The second one toString() returns a string representation of the Object. This method pretty much must be overridden in order for it to do much good. Typically it gives the value of all the fields for that particular instance of the class.

Method equals returns a boolean value and again must have the programmer provide his or her own implementation that is specific to the class for which it is used. Usually the first step is to test the class of the Object parameter by using the instanceof keyword and compare it to the current class. Usually the programmer will return false through a series of comparisons and have the last statement return true. The idea being that... in order for it to return true it must pass a series of comparisons that try to eliminate the possibility of it being the same object.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(t1.hashCode()+"\n"+t2.hashCode()); - Joe has explained it well
real fun goes here
System.out.println(t1.toString() == t2.toString()); - why this returns false.

To my understanding they have same value but they themselves are two different instances of String that's why its giving false. Please correct me if i am wrong.
Anyway rule of thumb for me is
== checks if reference is same
equals() checks if valued referred is meaningfully same.
 
Joe Ridener
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshvardhan ojha wrote:System.out.println(t1.hashCode()+"\n"+t2.hashCode()); - Joe has explained it well
real fun goes here
System.out.println(t1.toString() == t2.toString()); - why this returns false.

To my understanding they have same value but they themselves are two different instances of String that's why its giving false. Please correct me if i am wrong.
Anyway rule of thumb for me is
== checks if reference is same
equals() checks if valued referred is meaningfully same.


I cannot tell you exactly why; however, == vs. equals() when it comes to String objects will always favor method equals() from class Object. Either equals() or equalsIgnoreCase() for searching, sorting or parenthesis that require a boolean value (although equalsIgnoreCase() returns an integer value if I remember correctly, you would use the comparison operators in such a case < > <= >= and == along side equalsIgnoreCase() in that event). Perhaps the comparison operators only work with arethmetic expressions and method equals works on objects that do not have a numeric value (probably the case) and will always return false in the event == operator is used for non-numeric values.
 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ridener wrote:I cannot tell you exactly why; however, == vs. equals() when it comes to String objects will always favor method equals() from class Object...

Not quite sure what you mean here, but the fact is that you should almost always use equals() for reference types (ie, objects).
In fact, for beginners, I'd go so far as to say: never use '=='. EVER.

The reasons? Many:
1. You almost never want to know if two objects are the same object; you want to know if they have the same value.
2. If you absolutely must know whether two objects are identical, there is a specialized collection that caters for this: IdentityHashMap.
3. A properly wriiten equals() method will have the following statement as it's first:Hope you've got the message chaps: NEVER, EVER, EVER use '==' for anything except primitives - at least, not for the first couple of years.

The great thing about remembering that mantra is that you don't have to worry about how the language (or a particular class) deals with '=='.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic