• 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

Serialization

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone
I have read that you serialize to save the state of the object then why is it that if i print an object before serializing it in a file and if i print it after serializing it by reading it from a file ..both the object comes out to be different in the out put !!!
Thanks in Advance!!!
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since there are any number of things you could be doing wrong you will need to provide an SSCCE before anyone can really answer this.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class C
class Bclass A

Why the output coming is different ie.
com.B@10385c1
com.B@1632c2d
Both the objects are suppose to come out same.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote:
Why the output coming is different ie.
com.B@10385c1
com.B@1632c2d
Both the objects are suppose to come out same.



You have not created a toString() method for your class so those values do not represent the state of the object but are in some ways related to the address of the object in memory. Since you now have two different objects they have different addresses so have different values.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still not able to tell, SCJP book says on page 459 "serialization save the object & all of its instance variables."
If i am saving an object in a file and extracting it , all i want that both the objects should be same....here what i am not getting....overriding toString() will just return same String to all the objects of that class, which certainly do not answers my question.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote:I am still not able to tell, SCJP book says on page 459 "serialization save the object & all of its instance variables."


Which your code does.


If i am saving an object in a file and extracting it , all i want that both the objects should be same


Both objects have the same content but they are not the same object! The object read from the file is a clone of the object written to the file.


....here what i am not getting....overriding toString() will just return same String to all the objects of that class, which certainly do not answers my question.



When your write the object using
you automatically invoke the toString() method on the object but unless you override the toString() method you will invoke the toString() method of the parent class which in your case is class Object. The toString() method of class Object just creates a String that is in some way related to the address of the object in memory.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand what printing an object that does not have toString() and hashCode() overridden, does?

It will use the toString() from class java.lang.Object. Look in the API documentation what that method does: it prints the class name, an @, and the hash code of the object in hexadecimal.

If you also didn't override hashCode(), then the hash code will be whatever Object.hashCode() returns. What that number is, isn't specified. But in Oracle's JVM, it will be something that indicates the address of the object.

If you deserialize an object from a file, a new object is created that will be initialized with the values that are in the file. But there is no guarantee that the new object will be at the same location in memory as the original object. So there's no reason to expect that Object.hashCode() will return the same value as for the original object.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is misleading. Serialisation turns the state of the object and all its non‑transient instance fields into a series of bytes which can be sent over a network, stored in a file, etc. Except for enum constants, there is no guarantee that the original object will be recreated. In fact, a fresh instance is usually created. The Java Serialization Specification (ยง1.1=“overview”) specifically says new instance.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to write your own serialisation mechanism if you need to retrieve the exact same object.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie & Jesper de Jong for always correcting my doubts ... Respect !!!
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Richard for helping me out ....
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic