File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need Conformation about equals and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Conformation about equals and ==" Watch "Need Conformation about equals and ==" New topic
Author

Need Conformation about equals and ==

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
This is what i think:

1) == checks if both types being checked for == refers to a same object or not?It doesn't care about the values.Of course, if they refer to same object they will have same values.But what i meant about "It doesn't care about the values" means :
Integer i1=1;
Integer i2=new Integer(1);

now even though i1 and i2 hold same value i1==i2 will return false.

2)equals method cares only about the value and does not care about if the types that are checked for equals , refers to same object or not.
however there is an exception to this rule: If we are comparing wrapper classes variable using equals, then first it will check if they are of same type or not?If they are not of same type then it will return false.

when we use equals with strings then, only value is checked.
if we use == with strings then still rule 1 applies.

Am i correct with what i assume to be true?Are there any more rules regarding == and equals?

Also i have this code from enthuware's :


I thought that it prints true because both obj1 and obj2 will be null.(I know that both obj1 and obj2 point to same location).
But the explanation said that "it will return true because - Object class's equals() method just checks whether the two references are pointing to the same location or not. In this case they really are pointing to the same location because of obj2 = obj1; so it returns true. "
This makes my previous assumption about equals false?
So what is wrong here?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Rohan Deshmkh wrote:I thought that it prints true because both obj1 and obj2 will be null.

What makes you think they will be null? In any case, you can't invoke a method on a null reference.

2)equals method cares only about the value and does not care about if the types that are checked for equals

The equals(...) method, like any other method, executes the code contained in the method and returns whatever the logic dictates. It's not magically different or superior to any other method (except that Object mandates a special relationship between equals(...) and hashCode()).

Why don't you take a look at the implementation of equals(...) in the JDK classes you are interested in? In case you didn't know, you can find the sources in a file named src.zip in your JDK folder.


luck, db
There are no new questions, but there may be new answers.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3214
    
  49

Adding a little to what Darryl already said, an ideal implementation of equals method should check equality of the class types of the two instances and the equality between the significant attributes of the instances.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Ok i just had a look at equals method in object class and in String class.

So the equals method of object class return true if if they point to same objects.
And the equals method in String class return true if the sequence of characters is same in the types that are being checked for equals method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rohan Deshmkh wrote:This is what i think:

1) == checks if both types being checked for == refers to a same object or not?It doesn't care about the values.


Yes and no. It's true that equals doesn't care about the state (contents) of any object. However, the == operator always compares the values of its operands. However, the operands are never objects. They are either primitives or references. In the case of references, comparing those references' values amounts to "checking if they point to the same object, or are both null".


2)equals() method cares only about the value and does not care about if the types that are checked for equals , refers to same object or not.


The equals method does whatever it is coded to do. If it's written correctly--that is, if it follows the contract laid out in it documentation--it will return true of the two objects being compared are "equal" according to that class's semantics. What "equal" means depends on the class in question. It's up to the class author to decide.

[EDIT: Oops, guess I just repeated what DB said for #2. Oh well. ]
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Ok i got it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

Rohan Deshmkh wrote:1) == checks if both types being checked for == refers to a same object or not?It doesn't care about the values.
2)equals method cares only about the value and does not care about if the types that are checked for equals , refers to same object or not.

That's basically it. When used with objects (as opposed to primitives):

equals() is about equality (is this object "equal" to that one?).

'==' is about identity (is this object the same object as that one?).

However, having said that, you should probably basically forget it. There is almost NEVER a good reason for using '==' with objects, unless one of them is null.

For more information check out this page.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Conformation about equals and ==