Serialization of enum types and class upgrades in Java do not recommend the reason for enumeration __java

Source: Internet
Author: User
Tags serialization
In Java, serialization of an enum type differs from the serialization of other object types, to see what is different today. Let's look at how the enum we defined in Java is going to look like after being compiled.
Java code:
Java code public enum Fruitenum {APPLE, orage}
The code above defines a fruitenum type, which is the simplest form, and here we look at the compiled bytecode.
BYTE code:
Java Code    public final class com.taobao.tianxiao.fruitenum extends  java.lang.enum   ....   ....   ....   {   public static  final com.taobao.tianxiao.FruitEnum APPLE;      public static  final com.taobao.tianxiao.fruitenum orage;      static {};      Code:      Stack=4, Locals=0, Args_size=0       0:   new  #1; //class com/taobao/tianxiao/fruitenum       3:   dup      4:   ldc  #13;  //string  APPLE      6:   iconst_0      7:    invokespecial    #14; //method  "<init>":(ljava/lang/string;i) v    &NBSP;&NBSP;&NBSP;10:  putstatic    #18; //field apple:lcom/taobao/tianxiao/fruitenum;       13:  new  #1; //class com/taobao/tianxiao/fruitenum       16:  dup      17:  ldc  #20;  // string orage      19:  iconst_1      20:   invokespecial    #14; //method  "<init>":(ljava/lang/string;i) v      23:  putstatic    #21;  //field orage:lcom/taobao/tianxiao/ fruitenum;      26:  iconst_2      27:   anewarray    #1; //class com/taobao/tianxiao/fruitenum      30 :  dup      31:  iconst_0      32:   getstatic    #18; //field apple:lcom/taobao/tianxiao/fruitenum;       35:  aastore      36:  dup      37:   iconst_1      38:  getstatic    #21;  //field  ORAGE:Lcom/taobao/tianxiao/FruitEnum;      41:  aastore      42:  putstatic    #23;  //field enum$values:[lcom/taobao/ tianxiao/fruitenum;      45:  return     linenumbertable:        line 4: 0      line 3: 26       public static com.taobao.tianxiao.fruitenum[] values ();     Code:      Stack=5, Locals=3, Args_size=0       0:   getstatic    #23; //field enum$values:[lcom/taobao/tianxiao/fruitenum;       3:   dup      4:   astore_ 0      5:   iconst_0      6:    aload_0      7:   arraylength      8:    dup      9:   istore_1      10:   anewarray    #1; //class com/taobao/tianxiao/fruitenum       13:  dup      14:  astore_2       15:  iconst_0      16:  iload_1       17:  invokestatic     #31;  //method java/lang/system.arraycopy: ( Ljava/lang/object;iljaVA/LANG/OBJECT;II) v      20:  aload_2      21:   areturn     LineNumberTable:       line 1: 0       public static com.taobao.tianxiao.fruitenum valueof (java.lang.String);      Code:      Stack=2, Locals=1, Args_size=1       0:   ldc  #1; //class com/taobao/tianxiao/fruitenum       2:   aload_0      3:    invokestatic     #39;  //method java/lang/enum.valueof: (Ljava/lang/Class; ljava/lang/string) ljava/lang/enum;      6:   checkcast     #1; //class com/taobao/tianxiao/fruitenum      9:    areturn    &nbsP linenumbertable:       line 1: 0  }  
The byte code above has been removed from the constant pool portion, but even so, in our source code so simple a Fruitenum class, the compiler actually produced so many bytecode for us, wow Oh ~~~~~~~~
Looking at this code carefully, the compiler is creating a class for us that inherits from Java.lang.Enum, has two public, static, declared properties, and their type is the fruitenum we define. At the same time, the compiler also generates a static initializer, which is the static{} in the byte code, where the bytecode creates two Fruitenum objects and assigns them to both the Apple and Orange properties, and the calling constructor is defined in the Protected Enum (String name, int ordinal) method in Java.lang.Enum. After creating the two Fruitenum objects and assigning them to Apple and origin, we also created an array called enum$values, and then put Apple and origin in the order they were defined as the array.
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.