Why classes in Java are serialized

Source: Internet
Author: User
Tags object serialization


Why Serialize in Java?
http://zhidao.baidu.com/link?url=7_ Waq8eal28vcjpe5okm5y0bo4ainnqokhhrmi9xpszeoto5qf-gnbothpzu4e8jmxq2fyfju5kuugtsipyyn_ -- .- + the: 57 Breeze Xiao Xue | Browse2276Secondary programming language JAVA1 is a binary stream, and serialization is a binary stream, what is the difference? At the end of the computer is not all 0 and 1 save it? Or do not serialize can also be bad to save, is not serialized saved after good what? 2 For example, I wrote an article in a Notepad, is there any object here? It's all text, and I'm going to serialize it when I save it as a text file .?then I open this text file later to read into memory, and also to deserialize? 3 For example, to achieve the preservation of shopping information records, not to serialize, after the serialization of a text file or what file, can be opened with Notepad to see the text of the record information, not to say that after the serialization is a stream of characters, the text file is a character stream, that the record information opened should be garbled only right Ah, Because after serialization is a byte stream! Text File Open is naturally garbled? Do not say what is the concept of serialization what, I have seen several times, said to save the object structure or something, but still do not understand, for the ordinary string or not the difference between the serialization, do not understand, try to use examples or plain English to speak, not too professional, easy to understand can be shared: -- .- + Ten: -do not play "crazy rock Climbing" you are out! The question that the questioner adopts landlord is very interesting, but you have mistaken the application scenario of serialization and deserialization, we say that the serialization and deserialization is to Java (other object-oriented language may also have), the serialized entity is an object, the result is an object, not a formatted text, The shopping information you see in the Notepad is not the result of the object serialization, but the formatted text of the object output, and the real serialized object is not readable. In the actual use of object serialization, a scenario is to serialize the object to the persistent storage (local hard disk), we do not want to do file parsing at this time, and do not want to read this persistent file, when we need, you can directly use deserialization to save the file generated as an object, another scenario is in the network transmission process , the object will propagate on different hosts, and serialization will parse the object into a stream that is parsed by the peer, and this parsing process does not require anyone involved. Ordinary string is parsed object, have object to string to join the analytic logic, people can understand. The result of serialization is a file that only the Java virtual machine knows, and the person does not participate, but is used to save the object or transport. Did you solve your problem? Question: Does serialization have anything to do with strings in shopping information records? For example, I do not serialize, these words can not be saved to the hard disk? The other is the serialization of the object after the save where Ah, is the text in the shopping information record? Are they all saved to the hard drive? or where, for example, I opened the shopping information record only: XXX in one day shopping and so on, where there are objects ah, and look at the text file no difference Ah, then we always say that the object is saved I do not understand where this object, and the string has nothing to answer: serialization and string does not matter. The string is the output of some data in the object, and the text file you see contains only part of the object, and you can see that it is just a value. Like an object .Objectmonkey{String monkeyname="Wu Empty"; LongMonkeyage = +;} The string you output may be just this: Goku;10000but the object is a lot of information lost, such as Object name, field properties and so on. Serialization when the full input of the object to a place, such as a file, this file is not understood by humans, but in the deserialization, the file will be completed reading as an object, and the object above the same, so that we can be in the code to directly manipulate the object. is we clear?inquiry: Good!, that's it, but the other question is, where are the objects stored? Is it saved to my hard drive with my text file? For example, my text string is 5 string literal, by a character is 2 bytes, the size is 10 bytes, that if you say, the field name is also maintained, the content of the file will increase, it may not be 10 bytes, because you also saved the field property name what, it is not much of my hard disk space? Chase Answer: Save where you control, you want to export to the file system can also, other places also line. You said the size I do not know what to compare, the general situation will certainly be larger than the only save string, after all, there are other information need to store. will take up more space, but it's worth the cost of manually deserializing these costs. Cross-examine: I mean, do you serialize the content of the object that is saved later, is it also in the resulting text file? For example, the creation of a shopping information record. txt file, which if there are shopping information of the 4 words, the object can be seen in the text? Or can you only see the string? The saved object is actually in this file, but will not output the display, and most importantly, the size of the file on the computer or the size of 4 characters, but the object is also the existence of the disk space, but the system only shows the size of the string? Chase Answer: Text files are used to save text, is you in the code to control the generation of TXT. The serialized file generated by the object serialization is not related to the TXT file, what file format is saved to, and where it is stored, controlled by you after serialization. You should not think of txt store object, txt store can only be text, the result of object serialization is a serialized file, is the result of a file output stream. Cross-examine: Where is the serialized file saved, I want to see, in which corner of the computer is it stored? I'll see what's different from the text file. Answer: Import java.io.serializable;import java.io.*; Public classCat implements Serializable {PrivateString name; PublicCat () { This. Name ="New Cat";} PublicString GetName () {return This. Name; Public voidsetName (String name) { This. Name =name;} Public Static voidMain (string[] args) {cat cat=NewCat ();Try{FileOutputStream fos=NewFileOutputStream ("Catdemo.out");//This is where your serialized files are stored, controlled by yourself.ObjectOutputStream Oos =NewObjectOutputStream (FOS); System. out. println ("1>"+cat.getname ()); Cat.setname ("My Cat"); Oos.writeobject (cat); Oos.close ();}Catch(Exception ex) {ex.printstacktrace ();}Try{FileInputStream fis=NewFileInputStream ("Catdemo.out"); ObjectInputStream ois=NewObjectInputStream (FIS); Cat=(Cat) ois.readobject (); System. out. println ("2>"+cat.getname ()); Ois.close ();}Catch(Exception ex) {ex.printstacktrace ();}}} Output content: Catdemo.out Size:0. 06K has been Baidu security testing, easy download Click download Download Volume: -Cross-examine: the original. Out file is the serialized file Ah, that the saved shopping information records are all in the face? If it is here, then this file is not a. txt format, open look must be garbled? If the information record is not in the out file, it should be saved as a. txt file format in the specified path? This is the information record that can be opened to read? Are the files separate or together? Chase Answer: Saved shopping information in Catdemo.out, but you can not understand, to Java virtual machine to understand, because the serialized file itself is not for human reading preparation. If you want to export to txt, you need to manually specify the content and format of the output in the program so that you can read it. It's not two files, it could be multiple files, how many files you want to generate. Ask: That asks, if I also want to save output to TXT format. To manually add a path to the program: shopping information. txt is all right? So the catdemo.out on top of you don't have to erase it and keep it? So in the computer will produce a catdemo.out file, a text format of the shopping information,. txt? Catdemo.out has been serialized saved once, and the output of. txt is not required to serialize, the direct output character stream to the computer can be saved? Because there is already a serialized answer: Yes, you can understand that. In addition you need to serialize to see your own needs, serialization is for the program to read easily, there is no need to output. Cross-examine: meaning, you can not serialize also line, is the program read inconvenient, and previously in the memory of the object structure are all gone? The above also ask, in fact, in the Catdemo.out file contains only object information, should also contain string information it? Is should also contain txt inside the shopping information, and I output txt format, just extract the string part of the information from the Catdemo.out file to the user to see, the actual object content is still in the Catdemo.out save? Answer: Serialization is an operation, you do serialization is not necessary, you do serialization is because you want to save this object on the file system. Imagine a scene: You have developed a game, the game has characters, the characters have a property when he now has the money, when the user play half choose to save the game, you have two options: Save the user's money to txt, the next time the game load, then read the money from TXT, let the user continue to play. The object is serialized, the object is stored on the filesystem, and the next time you can play it, use deserialization to load. But the first solution has a disadvantage, if users know that the TXT saved money, you can change the money to 99999, the use of object serialization method will not. The last one, you don't understand the object in Java, the object is stored in memory, it doesn't exist because you serialized it,Serialization equals copying the data of the object, serializing it, and outputting it to the filesystem, the real object or the program. Just like you play the game to play half, suddenly want to save a file, and then the object serialized, but the game continues to Ah, the characters in it is not dead, indicating that the object is still in memory. is we clear?questioner rating Thank you!Comments ( One) | the 0babyly1314| Level four adoption rate 71%good at: Computer installed/shop Java related Server Software architecture design car purchase other similar issues .- ,14 What is Java serialization and how do I implement Java serialization? - .- AWhy serialize in -06java? When to serialize. the -- GenevaWhy serialize in -19java? When do I use serialization? A -- to-13 Questions about Java serialization .- A-22 What is serialization and how to serialize it in Java. +more questions about why classes in Java are serialized>> -- .- + Ten: atuser Adoption Do you know what is called serialization? Do you know the role of serialization? You can serialize the implementation of the Serializable interface, which is a serialized class that informs the program that the object of this class can read and write. Reading and writing is not only read and written in the text, but can also be read and written through other streams. A class that does not have a serialization cannot form a binary read-write stream. If you just tap and write, go inside a notepad. This is not the range of serialization. Serialization refers to the class object. Can you make sure that the text you're knocking on is the object of the class? Can you make sure what kind of object you're knocking on? The third question, there is no need to explain. You need to know what serialization is, and you won't be asking that question. Comment|3 4stale331x| From Team: Java Paradise | Level 10 Adoption Rate 47%good at: Java related data structure and algorithm database DB Server software recommended for you: Sort By default|Sort by Time other 3 answers -- .- + Ten: toZQHMFK |level five to enable JavaBean to pass I/o in network transmission then you have to implement the Serializable interface, which is a specification, just as you want Tomcat to invoke your servlet as you must implement its interface. To explain your question, say the flow: the first stream is divided into: Word stream (InputStream), character Stream (Reader), and you say the file that belongs to FileInputStream belongs to the byte stream, And the object that you're talking about is that ObjectInputStream is also a byte stream, but the write object must implement the Serializabled interface (which is the API documentation: only objects that support java.io.Serializable interfaces can be written to the stream. The classes for each serializable object are encoded, including the class name and class signature, the object's field value and the array value, and the closure of all other objects referenced from the original object. Cross-examine: FileInputStream is a byte stream, which is also a byte stream, is not that if I save the object, you can use this stream? But Java seems to have objects ah, that all can only use ObjectInputStream flow? The saved shopping record is a text file Ah, open can see the text information inside, also did not see the object ah, that the serialized save the object where Ah, also in the shopping record, together saved to the hard drive? Chase Answer: Text file belongs to character Stream (Reader), let you look at the API document words you understand: FileReader used to read the character stream. To read the raw byte stream, consider using FileInputStream. Big Brother still tangled ah ... I'm going to give you a familiar example. Everyone has used the Web container, now say WebLogic if we are going to register an account, we are first to enter the registration page, fill in the required fields, and finally click the Register button OK at this time the client sent a request to register, 1 The client first saves the information that the user fills in the request-------------------------------Client 2 Then serializes the request requests object, which holds the registration information that the user fills in the socket socket=NewSocket ("IP",10000); ObjectOutputStream OOS1=NewObjectOutputStream (Socket.getoutputstream ()); FileOutputStream out=NewFileOutputStream ("File.object");//landlord to see what is different from the text file ObjectOutputStream oos2 = new ObjectOutputStream (out);Oos1.writeobject (Request);//building mainly look at the serialization is how to add this sentenceOos2.writeobject (request); Oos.close (); 3 then the server, WebLogic, receives the request and deserializes it .-----------------------------Server ServerSocket SS=NewServerSocket (10000); while(true) {Socket=ss.accept (); ObjectInputStream Ois=NewObjectInputStream (Socket.getinputstream ()); Request Request=(Request) ois.readobject (); Ois.close ();} I'm so tired of writing ... Hope the landlord adopted!!! Reviews|4 0 -- .- + Ten: OneTESTHDG |level Four before I do not understand, but now I understand: Is this, if the Java object to be saved to the hard disk or to be transferred, must implement the Serializable interface, then this interface, what method is not, it is just an identity, and, it is best to have a sequence version number, such as PrivateStaticFinalLongSerialversionuid =1LI've done experiments before, I wrote a class person, and then person p=NewPerson (); by serialization, I use the code to save the P object in a file file! No serialization, absolutely impossible to save in the computer hard disk! Question: Does the text file open after you see only the string and the saved object information is not visible? But this file does have object information, but the system does not show you, only the string content is displayed? and the actual size of the file should be a string size+
Serialization is a mechanism for dealing with the flow of objects, so-called object flow is the flow of the object's contents. It is possible to read and write to a Fluidized object, or to transfer the streamed object between the networks. Serialization is a problem that is raised when reading and writing to an object stream. Serialization implementation: Implement the Serializable interface for the class that needs to be serialized, there is no method to implement it, implements serializable just to annotate that the object is serializable, and then use an output stream ( such as: FileOutputStream) to construct a ObjectOutputStream object, followed by the WriteObject (object obj) of the ObjectOutputStream object  method to write out (that is, save its state) the object with the parameter obj, and the input stream to restore. Serialization: Serialization is the process of converting an object into a format that is easy to transfer. For example, you can serialize an object and then use HTTP to transfer the object between the client and the server over the Internet. At the other end, deserialization reconstructs the object from the stream.    is a mechanism for object persistence. Specifically, the object should be serialized, the general program at run time, the object is generated, these objects disappear as the program stops running, but if we want to put some objects (because it is the object, so there are different characteristics) to save, after the program terminates, these objects still exist, You can read the values of these objects while the program is running again, or take advantage of these saved objects in other programs.    In this case, the serialization of the object is used. Only serialized objects can be stored on the storage device. An interface that needs to be inherited for the purpose of serializing an object is just a symbolic interface, which means that the object can be serialized and has no other purpose.    Object serialization is required because sometimes the object needs to be transmitted over the network, it needs this serialization processing, from the server hard disk to take the serialized object out, and then through the network to the client, and then by the client to the serialized object read into memory, to perform the corresponding processing. Object serialization is a feature of Java that allows objects to be written into a set of bytecode that, when read in other locations, creates a new object, and the state of the new object is exactly the same as the original object. In order to implement object serialization, it is necessary to have access to the private variables of the class, so that the object state can be saved and restored correctly. Accordingly, the object serialization API is able to restore these values to a private data member when the object is rebuilt. This is a challenge to access permissions for the Java language.    Usually used in the server client's object exchange above, the other is in the native storage. Object that is serialized.The most important use is to guarantee the integrity and transitivity of objects when passing, and saving objects (object). For example, to implement a serialized interface when transmitting over a network or saving an object as a file

Why classes in Java are serialized

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.