• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explation of Object Identity

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to 'Beginning Java.'
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot ... i got the point........ It is clearly explained.............

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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" ???
----------
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic