What is the role of object serialization in Java? What does Java RMI mean? __java

Source: Internet
Author: User
Tags object serialization serialization thread class

Turn from: http://hi.baidu.com/lanqibaoer/item/9366062187f7271642634a2f

What is the role of object serialization in Java.

1, the serialization is what.

This is simply to save the state of the various objects in memory, and to read the saved object state again. Although you can save the object states in a variety of ways,


But Java gives you a mechanism to preserve the state of objects better than yourself, and that is serialization.

2, under what circumstances need serialization.

A when you want to save the object in memory to a file or database.

b When you want to use sockets to transfer objects on the network

c When you want to transfer an object via RMI (Rmi->remote method invocation remote methods Call)

3. What happens when you serialize an object.

Before serialization, each object saved in the heap (HEAP) has a corresponding state, that is, an entity variable (instance ariable)

For example: Foo myfoo=new foo ();  Myfoo.setwidth (20); Myfoo.setheight (40);

When serialized through the following code, the values of the width and height instance variables (20,40) in the Myfoo object are saved to the Foo.ser file. This one can take it from


Read it in the file. Re-create the original object in the heap. Of course, the time to save is not just to save the value of the object's instance variable, but also to save a small amount of information, such as the type of class, so


Reply to the original object.

FileOutputStream fs=new FileOutputStream ("Foo.ser");

ObjectOutputStream os=new ObjectOutputStream (FS);

Os.write (Myfoo);

4, the implementation of serialization (save to a file steps) to illustrate


Java code

Import java.io.*;



public class Box implements Serializable

{

private int width;

private int height;


public void setwidth (int width) {

This.width = width;

}

public void setheight (int height) {

This.height = height;

}


public static void Main (string[] args) {

Box Mybox = new box ();

Mybox.setwidth (50);

Mybox.setheight (30);


try{

FileOutputStream fs = new FileOutputStream ("Foo.ser");

ObjectOutputStream OS = new ObjectOutputStream (FS);

Os.writeobject (Mybox);

Os.close ();

}catch (Exception ex) {

Ex.printstacktrace ();

}

}


}


5, related matters needing attention


(a) When a parent class is serialized, the subclass is automatically serialized, and the serializable interface is not required to be explicitly implemented;

b When an object's instance variable refers to another object, serialization of the object also serializes the referenced object;

c) Not all objects can be serialized, as for why not, there are many reasons, such as:


1. Security reasons, such as an object with a field such as Private,public, for an object to be transmitted, such as writing to a file, or for RMI transmission, etc., in the serialization of


The private domain of this object is not protected during row transmission.

2. Resource allocation reasons, such as the Socket,thread class, if they can be serialized, transmitted or saved, will not be able to reallocate their resources, and there is no


necessary to achieve this.


Serialization is the flow of objects to be transmitted over the network so that objects can be transmitted over the network, and each serializable object has a serialized ID that is uniquely identified by that object.

Serialization is used for the underlying data transfer, because the underlying communication is binary code, not an object, and serialization is a flag interface that tells the object of the class that it has the ability to serialize


Excerpt: Http://zhidao.baidu.com/question/155071231.html?fr=qrl&cid=870&index=1




What does Java RMI mean.

RMI is worth being a remote method call (invocation), a mechanism that enables objects on one Java virtual machine to invoke objects on another Java virtual machine


The method. Any object that can be invoked with this method must implement the remote interface. When you call such an object, its argument is "marshalled" and it is sent from the local virtual machine to the remote virtual


Machine (the remote virtual machine has a parameter of "unmarshalled"). When the method terminates, the results from the remote machine are grouped and the results are sent to the caller's virtual machine. If the method call causes the


Throws an exception, the exception is directed to the caller.

Can also be said to be distributed computing, can be called remotely method. Example: You have a Java-enabled cell phone and a desktop phone that has a very complex operation if you use a mobile phone


It may take a long time to calculate, but if the desktop is over in a few seconds, this is the RMI Shinton, using RMI to send requests from the phone and hand them over to the desktop


Compute and then the desktop returns calculations and results



http://xidianzxm.iteye.com/blog/1569972 Make a chart


http://www.blogjava.net/max/category/16130.html Max on Java about EE

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.