<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Understand The SerialVersionUID

    Refer To: http://www.mkyong.com/java-best-practices/understand-the-serialversionuid/

    If you have ever implemented Serializable interface, you must encounter this warning message

    The serializable class xxx does not declare a static final serialVersionUID field of type long

    So…what is serialVersionUID?

    The serialVersionUID is used as a version control in a Serializable class. If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various aspects of your Serializable class, as described in the Java(TM) Object Serialization Specification.

    1. SerialVersionUID Example

    The above statement is a bit hard to understand at the beginning (at least I did), let start an example to understand how Serializable class use SerialVersionUID to implement version control.

    1.1 Address.java

    A serializable class with a serialVersionUID of 1L.

    import java.io.Serializable;
     
    public class Address implements Serializable{
     
           
    private static final long serialVersionUID = 1L;
     
           String street;
           String country;
     
           
    public void setStreet(String street){
               
    this.street = street;
           }
     
           
    public void setCountry(String country){
               
    this.country = country;
           }
     
           
    public String getStreet(){
               
    return this.street;
           }
     
           
    public String getCountry(){
               
    return this.country;
           }
     
           @Override
           
    public String toString() {
               
    return new StringBuffer(" Street : ")
               .append(
    this.street)
               .append(
    " Country : ")
               .append(
    this.country).toString();
           }
    }

     

    1.2 WriteObject.java

    A simple class to write / serialize the Address object into a file – “c:\\address.ser”.

    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
     
    public class WriteObject{
     
        
    public static void main (String args[]) {
     
           Address address 
    = new Address();
           address.setStreet(
    "wall street");
           address.setCountry(
    "united states");
     
           
    try{
     
            FileOutputStream fout 
    = new FileOutputStream("c:\\address.ser");
            ObjectOutputStream oos 
    = new ObjectOutputStream(fout);   
            oos.writeObject(address);
            oos.close();
            System.out.println(
    "Done");
     
           }
    catch(Exception ex){
               ex.printStackTrace();
           } 
        }
    }

    1.3 ReadObject.java

    A simple class to read / deserialize the Address object from file – “c:\\address.ser”.

    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
     
    public class ReadObject{
     
       
    public static void main (String args[]) {
     
           Address address;
     
           
    try{
     
               FileInputStream fin 
    = new FileInputStream("c:\\address.ser");
               ObjectInputStream ois 
    = new ObjectInputStream(fin);
               address 
    = (Address) ois.readObject();
               ois.close();
     
               System.out.println(address);
     
           }
    catch(Exception ex){
               ex.printStackTrace(); 
           } 
       }
    }

    2. Testing

    Let do some testing to demonstrate the use of serialVersionUID.

    2.1 Same serialVersionUID

    Same serialVersionUID , there is no problem during the deserialization process

    javac Address.java
    javac WriteObject.java
    javac ReadObject.java
    java WriteObject
    java ReadObject
    Street : wall street Country : united states

    2.2 Different serialVersionUID

    In Address.java, change the serialVersionUID to 2L (it was 1L), and compile it again.

    javac Address.java
    java ReadObject
    java.io.InvalidClassException: Address; local class incompatible: 
    stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
             
            at ReadObject.main(ReadObject.java:14)

    The “InvalidClassException” will raise, because you write a serialization class with serialVersionUID “1L” but try to retrieve it back with updated serialization class, serialVersionUID “2L”.

    The serialVersionUID have to match during the serialization and deserialization process.

    3. What’s wrong with the default serialVersionUID?

    If no serialVersionUID is declared, JVM will use its own algorithm to generate a default SerialVersionUID, you can check the algorithm here.

    The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process.

    3.1 Client / Server environment

    - Client is using SUN’s JVM in Windows.
    - Server is using JRockit in Linux.

    The client sends a serializable class with default generated serialVersionUID (e.g 123L) to the server over socket, the server may generate a different serialVersionUID (e.g 124L) during deserialization process, and raises an unexpected InvalidClassExceptions.

    3.2 File / Database environment

    - App #1 is using SUN’s JVM in Windows.
    - App #2 is using JRockit in Linux.

    Serialization has allowed to save into a file or database. App #1 stores a serializable class into database by default generated serialVersionUID (e.g 123L), while App #2 may generate a different serialVersionUID (e.g 124L) during deserialization process, and raise an unexpected InvalidClassExceptions.

    You can check here for the List of the JVM implementation.

    4. How to generate serialVersionUID

    You can use JDK “serialver” or Eclipse IDE to generate serialVersionUID automatically, see detail.

    Conclusion

    SUN is highly recommended developers to declare the serialVersionUID in order to avoid the different JVM issue listed above, however I rather recommend you should understand what is serialization, how serialVersionUID implement version control and why your class need to use serialization. Understand the serialVersionUID concept is better than blindfold to any recommendation.

     

     



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-28 12:01 Chan Chen 閱讀(394) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Scala / Java

    主站蜘蛛池模板: 国产卡一卡二卡三免费入口| 波多野结衣在线免费观看| 欧洲人免费视频网站在线| 99国产精品视频免费观看| 老司机在线免费视频| 成人免费无码精品国产电影| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 日本最新免费网站| 成人无遮挡毛片免费看| 免费v片视频在线观看视频| 国产亚洲AV无码AV男人的天堂| 亚洲高清视频在线观看| 激情综合亚洲色婷婷五月| 白白色免费在线视频| 毛片在线全部免费观看| 国产又黄又爽又猛免费app| 免费成人在线观看| 亚洲午夜久久久精品影院| 亚洲欧洲国产综合AV无码久久| 国产成人1024精品免费| 2021国内精品久久久久精免费| 国产精品无码一二区免费| 亚洲gv猛男gv无码男同短文| 7777久久亚洲中文字幕| 一级**爱片免费视频| **毛片免费观看久久精品| 国产免费人成视频在线观看| 亚洲av无码成h人动漫无遮挡 | 亚洲aⅴ天堂av天堂无码麻豆| 黄色短视频免费看| 国产va免费精品观看精品| JLZZJLZZ亚洲乱熟无码| 亚洲人成电影青青在线播放| 一级毛片免费观看不收费| 在线看免费观看AV深夜影院| 亚洲一区二区三区乱码A| 亚洲日日做天天做日日谢| 中文字幕av无码不卡免费| 巨胸喷奶水视频www网免费| 亚洲成AV人片一区二区密柚| 亚洲国产欧洲综合997久久|