A question about Java serialization, see how much you know ————

Source: Internet
Author: User

Here's the question, and I'll answer it later.

Known classes have test and Test2, ask two times what the output of the main program (Serializeutil is just a serialized tool class)

Class Test

 Public classTestImplementsserializable{Private Static Final LongSerialversionuid = 1L; Private intnum; Private transientString str;  PublicTest () { This. num = 0xFFFF;  This. str = "string"; }     PublicTest (intnum, String str) {         This. num =num;  This. str =str; } @Override PublicString toString () {returnnum + "," +str; }}

Class Test2

 Public classTest2Implementsexternalizable,serializable{Private Static Final LongSerialversionuid = 1L; Private intnum; Private transientString str;  PublicTest2 () { This. num = 0xFFFF;  This. str = "Hello"; }     PublicTest2 (intnum, String str) {         This. num =num;  This. str =str; } @Override Public voidWriteexternal (ObjectOutput out)throwsioexception {out.write (num); } @Override Public voidReadexternal (ObjectInput in)throwsIOException, ClassNotFoundException { This. num =In.read (); } @Override PublicString toString () {returnnum + "," +str; }}

Two main programs:

     Public Static void Main (string[] args) {        new  Test ();         New File ("D:/test.bin");        Serializeutil.writeobject (file,test);         = (Test) serializeutil.readobject (file);        SYSTEM.OUT.PRINTLN (test);        System.out.println (test2);    }

     Public Static void Main (string[] args) {        new  Test2 ();         New File ("D:/test2.bin");        Serializeutil.writeobject (FILE,TEST2);         = (Test2) serializeutil.readobject (file);        System.out.println (test2);        System.out.println (t);    }

The code attached to Serializeutil can be ignored--

1  Public classSerializeutil {2 3      Public Static voidwriteobject (File file,object Object) {4FileOutputStream FileOutputStream =NULL;5         Try {6FileOutputStream =Newfileoutputstream (file);7ObjectOutputStream ObjectOutputStream =NewObjectOutputStream (fileoutputstream);8 Objectoutputstream.writeobject (object);9 Objectoutputstream.flush ();Ten}Catch(IOException e) { One e.printstacktrace (); A}finally { -             if(fileoutputstream!=NULL){ -                 Try { the fileoutputstream.close (); -}Catch(IOException e) {} -             } -         } +     } -  +      Public StaticObject readobject (file file) { AFileInputStream FileInputStream =NULL; at         Try { -FileInputStream =Newfileinputstream (file); -ObjectInputStream ObjectInputStream =NewObjectInputStream (fileinputstream); -             returnObjectinputstream.readobject (); -}Catch(classnotfoundexception|IOException E) { - e.printstacktrace (); in}finally { -             if(fileinputstream!=NULL){ to                 Try { + fileinputstream.close (); -}Catch(IOException e) {} the             } *         } $         return NULL;Panax Notoginseng     } -  the}
Serialization Tool Class

The answers are as follows:

/* First Main program: 65535,string65535,null the second main program: 65535,hello255,hello */
Answer

A question about Java serialization, see how much you know ————

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.