Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explation of Object Identity

 
Nuwan Arambage
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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


 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to 'Beginning Java.'
 
Campbell Ritchie
Sheriff
Pie
Posts: 49748
69
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Bartender
Pie
Posts: 6088
71
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks a lot ... i got the point........ It is clearly explained.............

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 6088
71
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic