Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Serialization of strings

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does serialization work when an object has multiple references to the same String object?

If serializable Object1 has references to serializable objects Object2 and Object3, and both Object2 and Object3 have a reference to String1, how many copies of String1 exist when Object1 is serialized and then deserialized? Is it still just one String object, or is it now two String objects with the same value?
 
Chris Brady
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found the answer. Here is the code to test it:



The result is:
Same string object
Same string object
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is true for all objects. An ObjectOutputStream keeps track of every object it writes. If it's asked to write an object a second or later time, it doesn't; instead, it writes a record that means "that same object over there, again." This is a source of frustration sometimes: if you write an object, change it, and write it again, then when the Stream is read, you read out two copies of the first state of the object.
 
Don't touch me. And dont' touch this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic