aspose file tools*
The moose likes Beginning Java and the fly likes Explation of Object Identity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explation of Object Identity" Watch "Explation of Object Identity" New topic
Author

Explation of Object Identity

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
I have read articles about object identity in object oriented context.

but still I couldn't understand it... Can somebody give me the definition and related example which clarify the

object identity...


secondly I want to know the difference between Identity and Equivalence in java with example




Thinker
Nuwan Arambage
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Moving to 'Beginning Java.'
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Do you mean about the difference between the same object found twice and two different objects which are identical? Have you read about the Object#equals() method?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

The identity of an object is simply that. Every object you create has its own unique identity. It doesn't matter if two objects are of the same class, hold the same references, have the same values; if they were created separately, they have unique identities. If two different variables hold a reference to the same object instance, the objects will be identical.

Equality means whether two (generally unique) objects are equal. Whether they are, depends on how the class defines equality.

Let's take a look at the book class:

book1 and book2 are two different copies of the same book "Nineteen eighty-four", writter by George Orwell. They are unique. However, the book class says that they are equal, because they have the same title and author.
book3 and book1 are obviously unequal, because they have different titles and authors.
book3 and book4 are not only equal, they are also identical, because they refer to the exact same copy of a book.
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
thanks a lot ... i got the point........ It is clearly explained.............

Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
Stephan van Hulst wrote:The identity of an object is simply that. Every object you create has its own unique identity. It doesn't matter if two objects are of the same class, hold the same references, have the same values; if they were created separately, they have unique identities. If two different variables hold a reference to the same object instance, the objects will be identical.

Equality means whether two (generally unique) objects are equal. Whether they are, depends on how the class defines equality.

Let's take a look at the book class:

book1 and book2 are two different copies of the same book "Nineteen eighty-four", writter by George Orwell. They are unique. However, the book class says that they are equal, because they have the same title and author.
book3 and book1 are obviously unequal, because they have different titles and authors.
book3 and book4 are not only equal, they are also identical, because they refer to the exact same copy of a book.




Thanks it really gave me good information
----------
Now, if you remove overriding "equal" method then

System.out.println(book1.equals(book2)); // Shall it return "false" ???
----------


Perform for today. Adapt for tomorrow.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Yes. Because then the equals method of the Object class is used, and it will say two objects are only equal if they are identical.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explation of Object Identity