aspose file tools*
The moose likes Beginning Java and the fly likes .equals() and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".equals() and "==" what is the output of the following code and why?" Watch ".equals() and "==" what is the output of the following code and why?" New topic
Author

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

sharma chetan
Greenhorn

Joined: Jul 20, 2012
Posts: 1
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

basically, == checks if both refer to the same object. whereas equals checks if they have the same value.

SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Jul 16, 2012
Posts: 71
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

Joined: Jul 26, 2007
Posts: 157
    
    1

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

Joined: Jul 16, 2012
Posts: 71
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

Joined: Mar 17, 2011
Posts: 7032
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: .equals() and "==" what is the output of the following code and why?
 
Similar Threads
cloning
Please Explain (Hashcode)
why i am getting this copile time error
equals hashcode doubt sjcp 1.4
A queastion about equals () & hashCode() methods