About 5 things you don't know about Java object serialization

Source: Internet
Author: User
Tags object serialization serialization

A few years ago, when I was working with a software team to write an application in the Java language, I realized the benefits of knowing a bit more about Java object serialization than the average programmer.

About a year ago, a developer who was responsible for managing all user settings for an application decided to store the user settings in a Hashtable and then serialize the Hashtable to disk for persistence. When the user changes the settings, the Hashtable is written back to disk.

This is an elegant, open setup system, but the system crashes when the team decides to migrate from Hashtable to the HashMap in the Java collections library.

The format of Hashtable and HashMap on disk is not identical and incompatible. Unless you run some type of data conversion utility (an extremely large task) for each persistent user setting, it seems that you will only be able to use Hashtable as your application's storage format in the future.

The team felt deadlocked, but it was just because they didn't know an important fact about Java serialization: Java serialization allows types to change over time. When I showed them how to automatically serialize the replacements, they finally completed the transition to HASHMAP as planned.

This is the first article in this series that is dedicated to revealing some of the useful little things about the Java platform-these little pieces of knowledge are not easy to understand, but will be useful for solving Java programming challenges sooner or later.

It's a good choice to start with the Java object serialization API, since it was in JDK 1.1 from the start. The 5 things that this article describes about serialization will convince you to revisit those standard Java APIs.

Introduction to Java serialization

Java object Serialization is one of a set of pioneering features introduced in JDK 1.1, used as a mechanism for converting the state of a Java object to a byte array for storage or transmission, and can still convert a byte array back to the original state of the Java object.

In fact, the idea of serialization is to "freeze" the object state, transfer the state of the object (write to disk, transfer over the network, and so on), and then "Unfreeze" the state and regain the available Java objects. All of this is a bit like magic, thanks to the Objectinputstream/objectoutputstream class, fully-guaranteed metadata, and the programmer's willingness to "participate" in the process by marking their classes with Serializable identity interfaces.

Listing 1 shows a person class that implements Serializable.

Listing 1. Serializable person

package Com.tedneward;

public class person
implements Java.io.Serializable
{
Public person (string fn, string ln, int a)
{
This.firstname = fn; this.lastname = ln; this.age = A;
}

Public String getfirstname () {return firstName;}
Public String Getlastname () {return lastName;}
public int Getage () {return age;}
Public Person Getspouse () {return spouse;}

public void Setfirstname (String value) {firstName = value;}
public void Setlastname (String value) {lastName = value;}
public void setage (int value) {age = value;}
public void Setspouse (person value) {spouse = value;}  

Public String toString ()
{
return "[person:firstname=" + FirstName +
"Lastname="    + LastName +
"age=" + Age +
"spouse=" + spouse.getfirstname () +
"]";
  

Private String firstName
PrivaTe String lastName;
Private int age;
Private person spouse;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.