Java serialization and de-serialization of Thinking?

java serialization process refers to the transition from a binary stream object needs to be serialized, and deserialized refers binary stream is converted to java object. So sometimes java stored in the database does not require serialization,

The computer system is essentially a binary file is stored, the nature of the database, what is it?

First, from the hardware to start

The core of five parts: computing, controller, memory, input devices, output devices

The Von Neumann computer theory, five parts in addition to the computer, the memory for storing instructions and data, an arithmetic operator and the logical operation is completed, the external personnel via the input / output device operating a computer and the internal computer is a binary data representing instructions .

Wherein it is composed of the specified operation code and address, the type of operation refers to an operation code, the address refers to a series of instructions of the program codes. Then for various data types in the database storage structure is how to type it?

 The current composition of the modern computer

 Second, the software system

Software macro is divided into: system software, application software

 See here, database management systems and operating systems belong to the system software.

 The operating system is covered in the first layer of software on hardware, computer hardware and software management resources. And to provide a good user interface. Operating system-related hardware, direct management of hardware resources, users with complete hardware-related operations, which greatly increased the use and utilization of hardware resources.

Operating system is a special system software, other system software runs on the operating system, you can get a lot of services provided by the operating system. That operating system is the interface between other software and hardware. In addition to general users using the computer operating system support, but also need to use

A large number of other system software and application software.

 Third, the database system

Database systems, the DBS, the computer system is a system configuration database is introduced, generally database systems, applications, database administrator, user configuration.

Database system to solve the problem of data stored separately.

Here's a bold guess: a database system for data storage and unified appearance, then to the computer store is unified binary nature of the conduct. Such that binary data type is non-oriented database, the database system is given good data type, then the rule database to store simply press,

As for the nature of the type of database, the internal database system will do a quick deal. But I think eventually binary, should only be stored in a binary computer. The extended file system, need to think about what is stored in the end? Currently limited thinking, assuming that we do not need to consider how to achieve internal database system

Want to memory map, we only relational database system what data types are supported. We store data in a database that is equivalent to the storage to the computer.

Storage-oriented Java development database, then how JAVA stored data to the database? Back to basics, serialization and de-serialization is why there?

Fourth, the programming language

First, what is the programming language? The most commonly used programming languages: C language, C ++, Java, C #, Python, PHP, JavaScript, Go, Object-c, Swift, assembly language, etc.

There are many languages, but you can achieve the same purpose, you can choose any language to control the computer.

We use language to control the computer, let the computer for us to do things such language is not a programming language. There are many programming languages, each of which has its own good aspects

 

 Different assembly language can be likened to the national language, in order to express the same meaning, may use different statements. Such as the World Hello meaning:

  • Chinese: Hello World;
  • English: Hello World
  • 法语: Hello everyone

In the kind of programming language, unity can be achieved

  • C Language: puts ( "C language Chinese network");
  • PHP: echo "C language Chinese net";
  • Java: System.out.println ( "C language Chinese network");

Programming language similar to human language, by the intuitive vocabulary composition, it must comply with its inherent format, otherwise the computer will not recognize

Fifth, the return of Java serialization and de-serialization

Sequence of: converting the target sequence is a sequence of bytes of the process stored on disk or network transmission is called objects.

Deserialize: restoring the sequence of bytes on the disk or network node to the object is called object deserialization words.

Java serialization of implementations of the interface: Serializable: Java.io.Serializable 

serialVersionUID: Good serialized version, those who implement the serialization interface class has reached a static variable represents the serialized version represented

Reference: https: //blog.csdn.net/xlgen157387/article/details/79840134

  • What is serialization and de-serialization
  • Java Serialization is the process of converting a Java object to a sequence of bytes, and to deserialize Java byte refers to the recovery process of Java objects
  • Serialization: Object Serialization The main use is in the transmission and preservation of objects of time to ensure the integrity and delivery of objects. Serialization is the object into an ordered byte stream for transmission or stored in a local file on the network. Serialized byte stream to save the state of Java objects and associated descriptive information.
  • Serialization is the core of the state of preservation and reconstruction of objects
  • Deserialized: client obtains target byte serialized stream file from the network or after, according to the stored byte stream and the description information object state, deserialized by the reconstructed object.
  • Essentially, the sequence of the physical state of the object is written according to a certain format to order byte stream is deserialized reconstructed object from an ordered byte stream to restore the state of the object.
  • Why serialization and de-serialization
  • We know that when two processes communicate remotely, you can send each other various types of data, including text, images, audio, video and so on. These data are in the form of a binary sequence transmitted on the network. So when the two Java-process communication, process transfer between objects can achieve it? The answer is yes, you need Java
  • Serialization and de-serialization. In other words, the sender need to convert the Java object is a sequence of bytes, and then transmitted on the network, on the other hand, the recipient needs to recover from the byte sequence of Java objects
  • Serialization benefits: 01 to achieve a data persistence, data storage can speak to disk. 02 using a sequence of remote communication, i.e., a communicating object on the network byte order
  • to sum up
  • 1. permanently and objects, a sequence of bytes stored object to a local file or database
  • 2 by making the sequence of the object in a byte stream is transmitted in the network and receiving
  • 3 by a sequence of passing between processes
  • Serialization algorithm
  • 1. The output of the metadata categories related object instance
  • 2. Description of the superclass recursively until no output class superclass
  • 3. Class metadata finished, start the actual data value output from the start of the object instance of the topmost superclass
  • 4. Data output from the top down recursive instance

Six, Java how to implement serialization and deserialization

 1. JDK class library serialization and deserialization API

java.io.ObjectOutputStream, writeObject (Object obj) method may serialize the object obj parameter specified, the resulting sequence of bytes written to a target output stream;

java.io.ObjectInputStream, readObject () method reads the source sequence of bytes in the input stream, then they become an object to deserialize, and return;

 2. implement the serialization requirements

Only by achieving Serializable or Externalizable interface object class can be serialized, or throw an exception! 

 3. Implementation

Assuming a User class, which is to serialize, can have the following three methods: 

( 1 ) only if the User class implements Serializable interface can be serialized and deserialized following manner 

ObjectOutputStream using the default serialization , non-transient instance variables User object serialization. 
ObjcetInputStream default deserialization mode, non-transient instance variables to the User object to be deserialized. 

( 2 ) only if the User class implements Serializable interface, and also defines the readObject (the ObjectInputStream in ) and the writeObject (ObjectOutputSteam OUT ), the following manner serialization and deserialization. 

User object ObjectOutputStream calls the writeObject (ObjectOutputStream OUT ) serialization method. 
ObjectInputStream calls readObject (ObjectInputStream User object in the method) is deserialized. 

( 3 ) If the class implements Externalnalizable User Interface and User class must implement the readExternal (an ObjectInput in) And the writeExternal (of ObjectOutput OUT ) method, the serialization and deserialization following manner. 

User object ObjectOutputStream calls the writeExternal (of ObjectOutput OUT )) of the method sequence. 
ObjectInputStream calls the User object readExternal (ObjectInput in ) method to deserialize.

Guess you like

Origin www.cnblogs.com/baizhuang/p/11916972.html