Today I have learned some basic principles and introduction of RPC calls, and the role of serializabel in the transfer of objects when remote methods are called is also more clear, so it is recorded

Many people think that when they write Java code, new POJO objects need to be serialized in order to be saved to disk. In fact, there is no necessary relationship between serialization and saving to disk.

  • When most people first learn the serialization operation, they usually understand that objects are serialized from byte stream files saved on disk, and then read backwards from byte stream as objects.
  • Converting a Java object to a common format is called serialization, and converting a common format to an object is called deserialization. Saving to disk is just one form of serialization.

Reasons for serialization

  • Store the state of an object in a storage medium so that an exact copy can be recreated later
  • Send objects from one application domain to another. The purpose of the Serializabel interface is to store objects to the byte stream and then restore them. Network transmission is based on the byte stream, so it can be both remote transmission and local recovery. (Ps: Without the serialization interface, the server does not recognize it as an object type, which is key.)

When do I need serialization

  • When you want to use a socket to send objects over the network;
  • When you want to write an object from memory to the hard disk;
  • When you want to transfer objects over RMI; (Calling methods remotely, such as RPC)

Compare Json with Serializabel

  • Json is smaller than Serializabel in length
  • In terms of speed, Json is faster than Serializabel, but there seems to be more than one serialization algorithm
  • Represents aspects, while Json generally represents strings that require additional processing if the front end wants to treat them as objects, serializabel restores directly

Json and Serializabel are selected

  • Serialize uses serialize, especially for storing objects. That’s what it’s all about.

  • Json can be used for object-independent data storage, such as arrays containing a large number of numbers. However, in this case, we may need to refactor the database.

  • JSON is used for data exchange, which is where the definition comes in.

  • Currently JSON is utF-8 encoded data

Java serialization

  • There are two ways to serialize Java. One is to implement the Serializable interface.
  • The other is to implement the Exteranlizable interface. To implement Exteranlizable, we need to rewrite the writeExternal and readExternal methods, which are more efficient than Serializable and can determine which attributes need to be serialized (even transient), but for a large number of objects, Or duplicate objects, it is inefficient.
  • Static variables are not serialized, even without the transient keyword modifier

An example of serializabel

object

import java.io.*;

public class Employee implements Serializable// Implements serialization{
    public String name;
    public String address;
    public transient int SSN;  //transient
    public int number;

    public void mailCheck(a) {
        System.out.println("Mailing a check to " + name
                + ""+ address); }}Copy the code

serialization

import java.io.*;

class SerializeDemo
{
    public static void main(String [] args)
    {
        Employee e = new Employee();
        e.name = "Reyan Ali";
        e.address = "Phokka Kuan, Ambehta Peer";
        e.SSN = 11122333;
        e.number = 101;
        try
        {
            FileOutputStream fileOut =
                    new FileOutputStream("C:\\Users\\Ryan\\Desktop\\obj.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(e);
            out.close();
            fileOut.close();
            System.out.printf("Serialized data is saved in obj.ser");
        }catch(IOException i) { i.printStackTrace(); }}}Copy the code

deserialization

import java.io.*;

public class Deserializable
{
    public static void main(String [] args)
    {
        Employee e = null;
        try
        {
            FileInputStream fileIn = new FileInputStream("C:\\Users\\Ryan\\Desktop\\obj.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        }catch(IOException i)
        {
            i.printStackTrace();
            return;
        }catch(ClassNotFoundException c)
        {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e.name);
        System.out.println("Address: " + e.address);
        System.out.println("SSN: " + e.SSN);
        System.out.println("Number: "+ e.number); }}Copy the code

eggsTransient keyword modified variable anti-sequence failed oh.