• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Conformation about equals and ==

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4322
236
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i got it.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic