SlideShare a Scribd company logo
Introduction to Class File
Format & Byte Code
By Douglas Q Hawkins

                     Wireless
                 Event Code: JM67
Topics
 Class File Format
 Byte Code
 Dynamic Generation of Byte Code
 Annotation Processing Tool
Class File Format
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE      Minor Version Major Version


                Constant Pool




                                               d
                                           ize
                                          gs

                                        on
                                       ar

                                     hr
                                    ar
                                  nc
                       su atile t/v




                       pu te d
  Flags    This Class Super Class




                           al sy
                       ce




                          iva te
                           tiv ct

                           l n
                       ab tfp


                       vo nsie




                       pr ec
                       fin r/
                       na ra
                    fa




                              ic
                       tra e




                       pr ic
                           pe
                   Interfaces


                           st
                  er




                           bl
                          ric




                          ot
                          at
                 int

                       st




                       st
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Class File Format
 CA   FE   BA    BE    Minor Version Major Version


                Constant Pool


  Flags    This Class Super Class
                   Interfaces
                   Fields

                  Methods
                  Attributes
Constant Pool
C        2   UTF   10                  HelloWorld
                                           C  4            UTF    16
                        “java/lang/Object”

UTF      6                         “<init>”                        UTF

    3             “()V”            UTF    4            “Code”
              M     3        9     N&T    5        6       UTF    4
             “main”                UTF   22
                      “([Ljava/lang/String;)V”

                               F     13       15       C     14    UTF

    16                     “java/lang/System”
Constant Pool (continued)
                      N&T    16     17 UTF        3        “out”
          UTF     21                “Ljava/io/PrintStream;”

                                                                           S

    19    UTF     11                     “Hello World”
                                                        M21           23
C        22     UTF     19             “java/io/PrintStream”

                                                  N&T       24   25    UTF

    7                                 “println”
UTF      21                       (Ljava/lang/String;)V
Field Format
  Flags   Name       Descriptor
              Attributes
Name       Flags
                        Field Format



int
     er
          fa
             ce
st
   ric
    Attributes




ab tfp
    s
na tra
    tiv ct
tra e
vo nsie
           Descriptor




    l n
su atile t/v
    pe      ar
fin r/          ar
    al sy         gs
st         nc
   at         hr
pr ic           on
   ot              ize
pr ec
   iva te              d
pu te d
    bl
       ic
Field Format
  Flags   Name        Descriptor
                   “name”
              Attributes
Field Format
  Flags   Name       Descriptor   “Ljava/lang/String;”
              Attributes
Field Format
  Flags   Name       Descriptor
              Attributes
Field Format
  Flags   Name       Descriptor
              Attributes

            ConstantValue
Method Format
 Flags   Name       Descriptor
             Attributes
Name       Flags




int
     er
          fa
             ce
                        Method Format




st
   ric
    Attributes




ab tfp
    s
na tra
    tiv ct
tra e
vo nsie
           Descriptor




    l n
su atile t/v
    pe      ar
fin r/          ar
    al sy         gs
st         nc
   at         hr
pr ic           on
   ot              ize
pr ec
   iva te              d
pu te d
    bl
       ic
Method Format
 Flags   Name        Descriptor
                   “main”
             Attributes
Method Format
 Flags   Name       Descriptor   “([Ljava/lang/String;)V”
             Attributes
Method Format
 Flags   Name       Descriptor
             Attributes
Method Format
 Flags   Name       Descriptor
             Attributes

             Exceptions
Method Format
 Flags   Name       Descriptor
             Attributes

             Exceptions


               Code
Constant Pool
C        2   UTF   10                  HelloWorld
                                           C  4            UTF    16
                        “java/lang/Object”

UTF      6                         “<init>”                        UTF

    3             “()V”            UTF    4            “Code”
              M     3        9     N&T    5        6       UTF    4
             “main”                UTF   22
                      “([Ljava/lang/String;)V”

                               F     13       15       C     14    UTF

    16                     “java/lang/System”
Constant Pool
1                2
    C        2       UTF   10                  HelloWorld
                                                3                    4
                                                   C  4                  UTF    16
                                “java/lang/Object”

5                                                                               6
    UTF      6                              “<init>”                                UTF
                                            7
        3               “()V”             UTF   4                “Code”
                 8                      9                            10
                    M     3        9      N&T   5           6            UTF    4
                                       11
                   “main”                 UTF  22
                            “([Ljava/lang/String;)V”

                                   12                       13                  14
                                        F       13     15        C         14       UTF

        16                         “java/lang/System”
Constant Pool
1                2
    C        2       UTF   10                  HelloWorld
                                                3                    4
                                                   C  4                  UTF    16
                                “java/lang/Object”

5                                                                               6
    UTF      6                              “<init>”                                UTF
                                            7
        3               “()V”             UTF   4                “Code”
                 8                      9                            10
                    M     3        9      N&T   5           6            UTF    4
                                       11
                   “main”                 UTF  22
                            “([Ljava/lang/String;)V”

                                   12                       13                  14
                                        F       13     15        C         14       UTF

        16                         “java/lang/System”
Constant Pool
1                2
    C        2       UTF   10                  HelloWorld
                                                3                    4
                                                   C  4                  UTF    16
                                “java/lang/Object”

5                                                                               6
    UTF      6                              “<init>”                                UTF
                                            7
        3               “()V”             UTF   4                “Code”
                 8                      9                            10
                    M     3        9      N&T   5           6            UTF    4
                                       11
                   “main”                 UTF  22
                            “([Ljava/lang/String;)V”

                                   12                       13                  14
                                        F       13     15        C         14       UTF

        16                         “java/lang/System”
Browsing Class File Format




       JClassLib Viewer
Byte Code
Stack Based Virtual Machine
0 iconst_1
              0   1   2   3
1 iconst_2
2 iadd
3 istore_0
4 iload_0
Stack Based Virtual Machine
0 iconst_1
                 0   1   2   3
1 iconst_2
2 iadd
3 istore_0
4 iload_0




             1
Stack Based Virtual Machine
0 iconst_1
                 0   1   2   3
1 iconst_2
2 iadd
3 istore_0
4 iload_0




             2
             1
Stack Based Virtual Machine
0 iconst_1
                   0   1   2   3
1 iconst_2
2 iadd
3 istore_0
4 iload_0




             1+2
Stack Based Virtual Machine
0 iconst_1
                 0   1   2   3
1 iconst_2
2 iadd
3 istore_0
4 iload_0




             3
Stack Based Virtual Machine
0 iconst_1
              0       1   2   3
1 iconst_2        3
2 iadd
3 istore_0
4 iload_0
Stack Based Virtual Machine
0 iconst_1
                 0       1   2   3
1 iconst_2           3
2 iadd
3 istore_0
4 iload_0




             3
Operation Types
 Load and Store
 Arithmetic and Logic
 Type Conversion
 Branch Instructions
 Object Creation and Manipulation
 Operand Stack
 Method Invocation
Data Types




                                   ac ts refix

                                          als
                                    ns e P

                                   ray oc
                                 Co Cod
                                 By ture



                                 Ar k / L
                                 St tan
                                    na
                                   te
                                Sig
                    boolean     Z
    Integer Types

                    byte        B b
                    char        C c
                    short       S s
                    int         I i
                    long        J l
                    float        F f
                    double      D d
                    reference   L a
                    void        V
Parameters and Local Variables
static int volume(
  int width,
  int depth,
  int height )
{
  int area = width * depth;
  int volume = area * height;
  return volume;
}
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
  int height )                   2 imul
{                                3 istore_3
  int area = width * depth;
                                 4 iload_3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                             e
  int height )




                                                      are t
                                                            h



                                                        lum
                                                         igh
                                                           h
                                                         pt


                                                          a
                                                        dt
{                                3 istore_3




                                                      de
                                                      he


                                                      vo
                                                      wi
                                                  0    1   2   3   4
  int area = width * depth;
                                 4 iload_3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                             e
  int height )




                                                      are t
                                                            h



                                                        lum
                                                         igh
                                                           h
                                                         pt


                                                          a
                                                        dt
{                                3 istore_3




                                                      de
                                                      he


                                                      vo
                                                      wi
                                                  0       1       2       3   4
  int area = width * depth;
                                 4 iload_3            1       2       3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3   4
  int area = width * depth;
                                 4 iload_3                1       2       3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn         1
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3   4
  int area = width * depth;
                                 4 iload_3                1       2       3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4   2
                                11ireturn         1
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3   4
  int area = width * depth;
                                 4 iload_3                1       2       3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn         2
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                             e
  int height )




                                                      are t
                                                            h



                                                        lum
                                                         igh
                                                           h
                                                         pt


                                                          a
                                                        dt
{                                3 istore_3




                                                      de
                                                      he


                                                      vo
                                                      wi
                                                  0       1       2       3       4
  int area = width * depth;
                                 4 iload_3            1       2       3       2
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3       4
  int area = width * depth;
                                 4 iload_3                1       2       3       2
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn         2
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3       4
  int area = width * depth;
                                 4 iload_3                1       2       3       2
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4   3
                                11ireturn         2
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3       4
  int area = width * depth;
                                 4 iload_3                1       2       3       2
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn         6
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                             e
  int height )




                                                      are t
                                                            h



                                                        lum
                                                         igh
                                                           h
                                                         pt


                                                          a
                                                        dt
{                                3 istore_3




                                                      de
                                                      he


                                                      vo
                                                      wi
                                                  0       1       2       3       4
  int area = width * depth;
                                 4 iload_3            1       2       3       2       6
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                                 e
  int height )




                                                          are t
                                                                h



                                                            lum
                                                             igh
                                                               h
                                                             pt


                                                              a
                                                            dt
{                                3 istore_3




                                                          de
                                                          he


                                                          vo
                                                          wi
                                                      0       1       2       3       4
  int area = width * depth;
                                 4 iload_3                1       2       3       2       6
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn         6
Parameters and Local Variables
static int volume(               0 iload_0
  int width,
                                 1 iload_1
  int depth,
                                 2 imul




                                                             e
  int height )




                                                      are t
                                                            h



                                                        lum
                                                         igh
                                                           h
                                                         pt


                                                          a
                                                        dt
{                                3 istore_3




                                                      de
                                                      he


                                                      vo
                                                      wi
                                                  0       1       2       3       4
  int area = width * depth;
                                 4 iload_3            1       2       3       2       6
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11ireturn
Static vs Virtual Methods
static int volume(               0 iload_0
  int width,                     1 iload_1
  int depth,
                                 2 imul




                                                              e
  int height )




                                                      are t
                                                      he h


                                                        lum
                                                            h

                                                         igh
                                                         pt


                                                          a
                                                         dt
{                                3 istore_3




                                                      de
                                                  wi




                                                      vo
                                                  0   1   2   3   4   5
  int area = width * depth;      4 iload_3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11 ireturn
Static vs Virtual Methods
int volume(                      0 iload_0
  int width,                     1 iload_1
  int depth,
                                 2 imul




                                                              e
  int height )




                                                      are t
                                                      he h


                                                        lum
                                                            h

                                                         igh
                                                         pt


                                                          a
                                                         dt
{                                3 istore_3




                                                      de
                                                  wi




                                                      vo
                                                  0   1   2   3   4   5
  int area = width * depth;      4 iload_3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11 ireturn
Static vs Virtual Methods
int volume(                      0 iload_0
  int width,                     1 iload_1
  int depth,
                                 2 imul




                                                          e
  int height )




                                                  are t
                                                  he h


                                                    lum
                                                        h

                                                     igh
                                                      pt


                                                       a
                                                     dt
                                                      s
{                                3 istore_3




                                                  de
                                                  thi
                                                  wi




                                                  vo
                                                  0   1   2   3   4   5
  int area = width * depth;      4 iload_3
  int volume = area * height;
  return volume;                 5 iload_2
}                                6 imul
                                 7 istore     4
                                 9 iload      4
                                11 ireturn
Static vs Virtual Methods
int volume(                      0 iload_1
  int width,                     1 iload_2
  int depth,
                                 2 imul




                                                         e
  int height )




                                                 are t
                                                 he h


                                                   lum
                                                       h

                                                    igh
                                                     pt


                                                      a
                                                    dt
                                                     s
{                                3 istore    4




                                                 de
                                                 thi
                                                 wi




                                                 vo
                                                 0   1   2   3   4   5
  int area = width * depth;      5 iload     4
  int volume = area * height;
  return volume;                 7 iload_3
}                                8 imul
                                 9 istore    5
                                11 iload     5
                                13 ireturn
Hello World
System.out.println( “Hello World” );
Hello World
System.out.println( “Hello World” );

 0   getstatic     System.out

 3   ldc           “Hello World”

 5   invokevirtual PrintStream.println

 8   return
Hello World
System.out.println( “Hello World” );
                                         0   1       2   3
 0   getstatic     System.out

 3   ldc           “Hello World”

 5   invokevirtual PrintStream.println
                                             “Hello World”
 8   return



                                                 System.out
Hello World
System.out.println( “Hello World” );
                                         0   1       2   3
 0   getstatic     System.out

 3   ldc           “Hello World”

 5   invokevirtual PrintStream.println
                                             “Hello World”
 8   return



                                                 System.out
Hello World
System.out.println( “Hello World” );
                                         0   1       2   3
 0   getstatic     System.out

 3   ldc           “Hello World”

 5   invokevirtual PrintStream.println
                                             “Hello World”
 8   return



                                                 System.out
Hello World




                                                      g
                                                s
                                                    ms
                                             thi
System.out.println( “Hello World” );
                                         0      1         2   3
 0   getstatic     System.out

 3   ldc           “Hello World”

 5   invokevirtual PrintStream.println
                                                    “Hello World”
 8   return



                                                    System.out
Types of Method Invocations
 invokestatic - invoke static methods
 invokevirtual - invoke instance method from class
 invokeinterface - invoke instance method from interface
 invokespecial - invoke <init> / invoke super method
New Object
 return new BigDecimal(“2.0”);
New Object
    return new BigDecimal(“2.0”);

0    new             BigDecimal

3    dup

4    ldc             “2.0”

6    invokespecial     BigDecimal.<init>

9    areturn
New Object
    return new BigDecimal(“2.0”);
                                           0   1    2      3
0    new             BigDecimal

3    dup

4    ldc             “2.0”
                                                   “2.0”
6    invokespecial     BigDecimal.<init>

9    areturn
New Object
    return new BigDecimal(“2.0”);
                                           0   1       2     3
0    new             BigDecimal

3    dup

4    ldc             “2.0”
                                                     “2.0”
6    invokespecial     BigDecimal.<init>

9    areturn

                                                   BigDecimal
New Object
    return new BigDecimal(“2.0”);
                                           0   1       2     3
0    new             BigDecimal

3    dup

4    ldc             “2.0”
                                                     “2.0”
6    invokespecial     BigDecimal.<init>

9    areturn

                                                   BigDecimal
New Object
    return new BigDecimal(“2.0”);
                                           0   1       2     3
0    new             BigDecimal

3    dup

4    ldc             “2.0”
                                                     “2.0”
6    invokespecial     BigDecimal.<init>

9    areturn

                                                   BigDecimal
New Object
    return new BigDecimal(“2.0”);
                                           0   1       2     3
0    new             BigDecimal

3    dup

4    ldc             “2.0”
                                                     “2.0”
6    invokespecial     BigDecimal.<init>

9    areturn

                                                   BigDecimal
If / Else
static boolean isPositive( int val ){
  if ( val > 0 ) {
    return true;
  } else {
    return false;
  }
}
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn

static boolean isPositive( int val ){
  return ( val > 0 );
}
If / Else
static boolean isPositive( int val ){
  if ( val > 0 ) {
    return true;
  } else {
    return false;
  }
}
static boolean isPositive( int val ){
  return ( val > 0 );
}
If / Else
static boolean isPositive( int val ){          0 iload_0




                                        test
  if ( val > 0 ) {                             1 ifle        +5 //6
    return true;
                                               4 iconst_1
  } else {




                                        if
    return false;                              5 ireturn
  }                                            6 iconst_0




                                        else
}                                              7 ireturn

static boolean isPositive( int val ){
  return ( val > 0 );
}
For Loop
static int sum( int min, int max ){
  int sum = 0;
  for ( int i=min; i<max; ++i ){
    sum += i;
  }
  return sum;
}
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
before
                                                            0 iconst_0
   For Loop




                                      init & test loop
                                                            1 istore_2
                                                            2 iload_1
static int sum( int min, int max ){                         3 istore_3
  int sum = 0;                                              4 goto        +10 //14
  for ( int i=min; i<max; ++i ){                            7 iload_2




                                            loop body
    sum += i;                                               8 iload_3
  }                                                         9 iadd
  return sum;                                              10 istore_2
}


                                            inc
                                                           11 iinc        3 by 1
                                                           14 iload_3
                                            test           15 iload_1
                                                           16 if_icmplt   -9 //7
                                                           19 iload_2
                                      after
                                      loop



                                                           20 ireturn
Exception Handling
static int read( InputStream in ) {
  try {
    return in.read();
  } catch ( IOException e ) {
    return -1;
  } finally {
    IoUtils.closeQuietly( in );
  }
}
0 aload_0
Exception Handling                                      1 invokevirtual InputStream.read




                                      try / finally
static int read( InputStream in ) {                     4 istore_1
  try {                                                 5 aload_0
    return in.read();                                   6 invokestatic IoUtils.closeQuietly
  } catch ( IOException e ) {                           9 iload_1
    return -1;                                         10 ireturn
  } finally {
                                                       11 pop
    IoUtils.closeQuietly( in );



                                      catch / finally
                                                       12 aload_0
  }
                                                       13 invokestatic IoUtils.closeQuietly
}
                                                       16 iconst_m1
                                                       17 ireturn
                                                       18 astore_2
                                                       19 aload_0
                                      finally



                                                       20 invokestatic IoUtils.closeQuietly
                                                       23 aload_2
                                                       24 athrow
0 aload_0
Exception Handling                                      1 invokevirtual InputStream.read




                                      try / finally
static int read( InputStream in ) {                     4 istore_1
  try {                                                 5 aload_0
    return in.read();                                   6 invokestatic IoUtils.closeQuietly
  } catch ( IOException e ) {                           9 iload_1
    return -1;                                         10 ireturn
  } finally {
                                                       11 pop
    IoUtils.closeQuietly( in );



                                      catch / finally
                                                       12 aload_0
  }
                                                       13 invokestatic IoUtils.closeQuietly
}
                                                       16 iconst_m1
                                                       17 ireturn
                                                       18 astore_2
                                                       19 aload_0
                                      finally



                                                       20 invokestatic IoUtils.closeQuietly
                                                       23 aload_2
                                                       24 athrow
0 aload_0
Exception Handling                                       1 invokevirtual InputStream.read




                                       try / finally
static int read( InputStream in ) {                      4 istore_1
  try {                                                  5 aload_0
    return in.read();                                    6 invokestatic IoUtils.closeQuietly
  } catch ( IOException e ) {                            9 iload_1
    return -1;                                          10 ireturn
  } finally {
                                                        11 pop
    IoUtils.closeQuietly( in );



                                       catch / finally
                                                        12 aload_0
  }
                                                        13 invokestatic IoUtils.closeQuietly
}
                                                        16 iconst_m1
                                                        17 ireturn
         Exception Table
                                                        18 astore_2
start   end    handler    Exception
                                                        19 aload_0
  0      5       11      IOException
                                       finally



                                                        20 invokestatic IoUtils.closeQuietly
  0      5       18         any
                                                        23 aload_2
 11      12      18         any                         24 athrow
0 aload_0
Exception Handling                                       1 invokevirtual InputStream.read




                                       try / finally
static int read( InputStream in ) {                      4 istore_1
  try {                                                  5 aload_0
    return in.read();                                    6 invokestatic IoUtils.closeQuietly
  } catch ( IOException e ) {                            9 iload_1
    return -1;                                          10 ireturn
  } finally {
                                                        11 pop
    IoUtils.closeQuietly( in );



                                       catch / finally
                                                        12 aload_0
  }
                                                        13 invokestatic IoUtils.closeQuietly
}
                                                        16 iconst_m1
                                                        17 ireturn
         Exception Table
                                                        18 astore_2
start   end    handler    Exception
                                                        19 aload_0
  0      5       11      IOException
                                       finally



                                                        20 invokestatic IoUtils.closeQuietly
  0      5       18         any
                                                        23 aload_2
 11      12      18         any                         24 athrow
More Attributes...
 Java 1.2+
   InnerClass

   Synthetic

   Deprecated
More Attributes...
 Java 1.2+
   InnerClass

   Synthetic

   Deprecated


Debugging
  LineNumberTable

  LocalVariableTable

  SourceFile
More Attributes...
 Java 1.2+             Java 5+
   InnerClass            Signature
   Synthetic             RuntimeVisibleAnnotations
   Deprecated            RuntimeInvisibleAnnotations

Debugging
  LineNumberTable

  LocalVariableTable

  SourceFile
Byte Code Generation
Resources
The Java Virtual Machine Specification
http://java.sun.com/docs/books/jvms/
JClassLib Viewer
http://www.ej-technologies.com/products/jclasslib/
overview.html
Maxine VM
http://labs.oracle.com/projects/maxine/
New Features in Java 6 - Annotation Processing
http://www.javabeat.net/articles/14-java-60-features-
part-2-pluggable-annotation-proce-1.html

More Related Content

PPT
F# Intro for Scala Developers
PDF
JCache - Gimme Caching - JavaLand
PDF
JVM Internals - NEJUG Nov 2010
PDF
In-Memory Distributed Computing - Porto Tech Hub
PDF
Big Data, Fast Data - MapReduce in Hazelcast
PDF
Gimme Caching - The JCache Way
PDF
Gimme Caching, the Hazelcast JCache Way
PDF
My Old Friend Malloc
F# Intro for Scala Developers
JCache - Gimme Caching - JavaLand
JVM Internals - NEJUG Nov 2010
In-Memory Distributed Computing - Porto Tech Hub
Big Data, Fast Data - MapReduce in Hazelcast
Gimme Caching - The JCache Way
Gimme Caching, the Hazelcast JCache Way
My Old Friend Malloc

Viewers also liked (8)

PDF
The Delivery Hero - A Simpsons As A Service Storyboard
PDF
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
PDF
Distributed computing with Hazelcast - JavaOne 2014
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PDF
A Post-Apocalyptic sun.misc.Unsafe World
PDF
Understanding Garbage Collection
PDF
In-Memory Computing - Distributed Systems - Devoxx UK 2015
PDF
Hazelcast - In-Memory DataGrid
The Delivery Hero - A Simpsons As A Service Storyboard
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Distributed computing with Hazelcast - JavaOne 2014
JVM Mechanics: When Does the JVM JIT & Deoptimize?
A Post-Apocalyptic sun.misc.Unsafe World
Understanding Garbage Collection
In-Memory Computing - Distributed Systems - Devoxx UK 2015
Hazelcast - In-Memory DataGrid
Ad

Similar to Introduction to Class File Format & Byte Code (20)

PDF
JVM Internals - NHJUG Jan 2012
PPT
Let's talk about java class file
PDF
JVM Internals by Douglas Hawkins
PDF
Domain-Specific Type Systems
PDF
JCL SORT TOOL
DOC
Interface Vs Abstact
PPTX
PPT
Core java
PDF
Csharp Online 100
PPTX
Silicon Valley Code Camp - Do you C what I C
PDF
Ecma 262
PDF
Ecma 262
DOC
C++ Online Training
PDF
Rc016 csharp online
PPT
Hive Object Model
PDF
Rc018 corenet online
PPTX
PDF
2008-12-21 Rubinius
PPT
Java Basics
JVM Internals - NHJUG Jan 2012
Let's talk about java class file
JVM Internals by Douglas Hawkins
Domain-Specific Type Systems
JCL SORT TOOL
Interface Vs Abstact
Core java
Csharp Online 100
Silicon Valley Code Camp - Do you C what I C
Ecma 262
Ecma 262
C++ Online Training
Rc016 csharp online
Hive Object Model
Rc018 corenet online
2008-12-21 Rubinius
Java Basics
Ad

More from Doug Hawkins (7)

PDF
JVM Mechanics: Understanding the JIT's Tricks
PDF
ReadyNow: Azul's Unconventional "AOT"
PDF
Java Performance Puzzlers
PDF
JVM Mechanics
PDF
Concurrency Concepts in Java
PDF
Inside Android's Dalvik VM - NEJUG Nov 2011
PDF
JVM Internals - Garbage Collection & Runtime Optimizations
JVM Mechanics: Understanding the JIT's Tricks
ReadyNow: Azul's Unconventional "AOT"
Java Performance Puzzlers
JVM Mechanics
Concurrency Concepts in Java
Inside Android's Dalvik VM - NEJUG Nov 2011
JVM Internals - Garbage Collection & Runtime Optimizations

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Introduction to Class File Format & Byte Code

  • 1. Introduction to Class File Format & Byte Code By Douglas Q Hawkins Wireless Event Code: JM67
  • 2. Topics Class File Format Byte Code Dynamic Generation of Byte Code Annotation Processing Tool
  • 4. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 5. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 6. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 7. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 8. Class File Format CA FE BA BE Minor Version Major Version Constant Pool d ize gs on ar hr ar nc su atile t/v pu te d Flags This Class Super Class al sy ce iva te tiv ct l n ab tfp vo nsie pr ec fin r/ na ra fa ic tra e pr ic pe Interfaces st er bl ric ot at int st st Fields Methods Attributes
  • 9. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 10. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 11. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 12. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 13. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 14. Class File Format CA FE BA BE Minor Version Major Version Constant Pool Flags This Class Super Class Interfaces Fields Methods Attributes
  • 15. Constant Pool C 2 UTF 10 HelloWorld C 4 UTF 16 “java/lang/Object” UTF 6 “<init>” UTF 3 “()V” UTF 4 “Code” M 3 9 N&T 5 6 UTF 4 “main” UTF 22 “([Ljava/lang/String;)V” F 13 15 C 14 UTF 16 “java/lang/System”
  • 16. Constant Pool (continued) N&T 16 17 UTF 3 “out” UTF 21 “Ljava/io/PrintStream;” S 19 UTF 11 “Hello World” M21 23 C 22 UTF 19 “java/io/PrintStream” N&T 24 25 UTF 7 “println” UTF 21 (Ljava/lang/String;)V
  • 17. Field Format Flags Name Descriptor Attributes
  • 18. Name Flags Field Format int er fa ce st ric Attributes ab tfp s na tra tiv ct tra e vo nsie Descriptor l n su atile t/v pe ar fin r/ ar al sy gs st nc at hr pr ic on ot ize pr ec iva te d pu te d bl ic
  • 19. Field Format Flags Name Descriptor “name” Attributes
  • 20. Field Format Flags Name Descriptor “Ljava/lang/String;” Attributes
  • 21. Field Format Flags Name Descriptor Attributes
  • 22. Field Format Flags Name Descriptor Attributes ConstantValue
  • 23. Method Format Flags Name Descriptor Attributes
  • 24. Name Flags int er fa ce Method Format st ric Attributes ab tfp s na tra tiv ct tra e vo nsie Descriptor l n su atile t/v pe ar fin r/ ar al sy gs st nc at hr pr ic on ot ize pr ec iva te d pu te d bl ic
  • 25. Method Format Flags Name Descriptor “main” Attributes
  • 26. Method Format Flags Name Descriptor “([Ljava/lang/String;)V” Attributes
  • 27. Method Format Flags Name Descriptor Attributes
  • 28. Method Format Flags Name Descriptor Attributes Exceptions
  • 29. Method Format Flags Name Descriptor Attributes Exceptions Code
  • 30. Constant Pool C 2 UTF 10 HelloWorld C 4 UTF 16 “java/lang/Object” UTF 6 “<init>” UTF 3 “()V” UTF 4 “Code” M 3 9 N&T 5 6 UTF 4 “main” UTF 22 “([Ljava/lang/String;)V” F 13 15 C 14 UTF 16 “java/lang/System”
  • 31. Constant Pool 1 2 C 2 UTF 10 HelloWorld 3 4 C 4 UTF 16 “java/lang/Object” 5 6 UTF 6 “<init>” UTF 7 3 “()V” UTF 4 “Code” 8 9 10 M 3 9 N&T 5 6 UTF 4 11 “main” UTF 22 “([Ljava/lang/String;)V” 12 13 14 F 13 15 C 14 UTF 16 “java/lang/System”
  • 32. Constant Pool 1 2 C 2 UTF 10 HelloWorld 3 4 C 4 UTF 16 “java/lang/Object” 5 6 UTF 6 “<init>” UTF 7 3 “()V” UTF 4 “Code” 8 9 10 M 3 9 N&T 5 6 UTF 4 11 “main” UTF 22 “([Ljava/lang/String;)V” 12 13 14 F 13 15 C 14 UTF 16 “java/lang/System”
  • 33. Constant Pool 1 2 C 2 UTF 10 HelloWorld 3 4 C 4 UTF 16 “java/lang/Object” 5 6 UTF 6 “<init>” UTF 7 3 “()V” UTF 4 “Code” 8 9 10 M 3 9 N&T 5 6 UTF 4 11 “main” UTF 22 “([Ljava/lang/String;)V” 12 13 14 F 13 15 C 14 UTF 16 “java/lang/System”
  • 34. Browsing Class File Format JClassLib Viewer
  • 36. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 2 iadd 3 istore_0 4 iload_0
  • 37. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 2 iadd 3 istore_0 4 iload_0 1
  • 38. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 2 iadd 3 istore_0 4 iload_0 2 1
  • 39. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 2 iadd 3 istore_0 4 iload_0 1+2
  • 40. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 2 iadd 3 istore_0 4 iload_0 3
  • 41. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 3 2 iadd 3 istore_0 4 iload_0
  • 42. Stack Based Virtual Machine 0 iconst_1 0 1 2 3 1 iconst_2 3 2 iadd 3 istore_0 4 iload_0 3
  • 43. Operation Types Load and Store Arithmetic and Logic Type Conversion Branch Instructions Object Creation and Manipulation Operand Stack Method Invocation
  • 44. Data Types ac ts refix als ns e P ray oc Co Cod By ture Ar k / L St tan na te Sig boolean Z Integer Types byte B b char C c short S s int I i long J l float F f double D d reference L a void V
  • 45. Parameters and Local Variables static int volume( int width, int depth, int height ) { int area = width * depth; int volume = area * height; return volume; }
  • 46. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, int height ) 2 imul { 3 istore_3 int area = width * depth; 4 iload_3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 47. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 48. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 49. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn 1
  • 50. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 2 11ireturn 1
  • 51. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn 2
  • 52. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 53. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn 2
  • 54. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 3 11ireturn 2
  • 55. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn 6
  • 56. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 6 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 57. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 6 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn 6
  • 58. Parameters and Local Variables static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t h lum igh h pt a dt { 3 istore_3 de he vo wi 0 1 2 3 4 int area = width * depth; 4 iload_3 1 2 3 2 6 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11ireturn
  • 59. Static vs Virtual Methods static int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t he h lum h igh pt a dt { 3 istore_3 de wi vo 0 1 2 3 4 5 int area = width * depth; 4 iload_3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11 ireturn
  • 60. Static vs Virtual Methods int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t he h lum h igh pt a dt { 3 istore_3 de wi vo 0 1 2 3 4 5 int area = width * depth; 4 iload_3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11 ireturn
  • 61. Static vs Virtual Methods int volume( 0 iload_0 int width, 1 iload_1 int depth, 2 imul e int height ) are t he h lum h igh pt a dt s { 3 istore_3 de thi wi vo 0 1 2 3 4 5 int area = width * depth; 4 iload_3 int volume = area * height; return volume; 5 iload_2 } 6 imul 7 istore 4 9 iload 4 11 ireturn
  • 62. Static vs Virtual Methods int volume( 0 iload_1 int width, 1 iload_2 int depth, 2 imul e int height ) are t he h lum h igh pt a dt s { 3 istore 4 de thi wi vo 0 1 2 3 4 5 int area = width * depth; 5 iload 4 int volume = area * height; return volume; 7 iload_3 } 8 imul 9 istore 5 11 iload 5 13 ireturn
  • 64. Hello World System.out.println( “Hello World” ); 0 getstatic System.out 3 ldc “Hello World” 5 invokevirtual PrintStream.println 8 return
  • 65. Hello World System.out.println( “Hello World” ); 0 1 2 3 0 getstatic System.out 3 ldc “Hello World” 5 invokevirtual PrintStream.println “Hello World” 8 return System.out
  • 66. Hello World System.out.println( “Hello World” ); 0 1 2 3 0 getstatic System.out 3 ldc “Hello World” 5 invokevirtual PrintStream.println “Hello World” 8 return System.out
  • 67. Hello World System.out.println( “Hello World” ); 0 1 2 3 0 getstatic System.out 3 ldc “Hello World” 5 invokevirtual PrintStream.println “Hello World” 8 return System.out
  • 68. Hello World g s ms thi System.out.println( “Hello World” ); 0 1 2 3 0 getstatic System.out 3 ldc “Hello World” 5 invokevirtual PrintStream.println “Hello World” 8 return System.out
  • 69. Types of Method Invocations invokestatic - invoke static methods invokevirtual - invoke instance method from class invokeinterface - invoke instance method from interface invokespecial - invoke <init> / invoke super method
  • 70. New Object return new BigDecimal(“2.0”);
  • 71. New Object return new BigDecimal(“2.0”); 0 new BigDecimal 3 dup 4 ldc “2.0” 6 invokespecial BigDecimal.<init> 9 areturn
  • 72. New Object return new BigDecimal(“2.0”); 0 1 2 3 0 new BigDecimal 3 dup 4 ldc “2.0” “2.0” 6 invokespecial BigDecimal.<init> 9 areturn
  • 73. New Object return new BigDecimal(“2.0”); 0 1 2 3 0 new BigDecimal 3 dup 4 ldc “2.0” “2.0” 6 invokespecial BigDecimal.<init> 9 areturn BigDecimal
  • 74. New Object return new BigDecimal(“2.0”); 0 1 2 3 0 new BigDecimal 3 dup 4 ldc “2.0” “2.0” 6 invokespecial BigDecimal.<init> 9 areturn BigDecimal
  • 75. New Object return new BigDecimal(“2.0”); 0 1 2 3 0 new BigDecimal 3 dup 4 ldc “2.0” “2.0” 6 invokespecial BigDecimal.<init> 9 areturn BigDecimal
  • 76. New Object return new BigDecimal(“2.0”); 0 1 2 3 0 new BigDecimal 3 dup 4 ldc “2.0” “2.0” 6 invokespecial BigDecimal.<init> 9 areturn BigDecimal
  • 77. If / Else static boolean isPositive( int val ){ if ( val > 0 ) { return true; } else { return false; } }
  • 78. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn
  • 79. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn
  • 80. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn
  • 81. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn
  • 82. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn static boolean isPositive( int val ){ return ( val > 0 ); }
  • 83. If / Else static boolean isPositive( int val ){ if ( val > 0 ) { return true; } else { return false; } } static boolean isPositive( int val ){ return ( val > 0 ); }
  • 84. If / Else static boolean isPositive( int val ){ 0 iload_0 test if ( val > 0 ) { 1 ifle +5 //6 return true; 4 iconst_1 } else { if return false; 5 ireturn } 6 iconst_0 else } 7 ireturn static boolean isPositive( int val ){ return ( val > 0 ); }
  • 85. For Loop static int sum( int min, int max ){ int sum = 0; for ( int i=min; i<max; ++i ){ sum += i; } return sum; }
  • 86. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 87. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 88. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 89. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 90. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 91. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 92. before 0 iconst_0 For Loop init & test loop 1 istore_2 2 iload_1 static int sum( int min, int max ){ 3 istore_3 int sum = 0; 4 goto +10 //14 for ( int i=min; i<max; ++i ){ 7 iload_2 loop body sum += i; 8 iload_3 } 9 iadd return sum; 10 istore_2 } inc 11 iinc 3 by 1 14 iload_3 test 15 iload_1 16 if_icmplt -9 //7 19 iload_2 after loop 20 ireturn
  • 93. Exception Handling static int read( InputStream in ) { try { return in.read(); } catch ( IOException e ) { return -1; } finally { IoUtils.closeQuietly( in ); } }
  • 94. 0 aload_0 Exception Handling 1 invokevirtual InputStream.read try / finally static int read( InputStream in ) { 4 istore_1 try { 5 aload_0 return in.read(); 6 invokestatic IoUtils.closeQuietly } catch ( IOException e ) { 9 iload_1 return -1; 10 ireturn } finally { 11 pop IoUtils.closeQuietly( in ); catch / finally 12 aload_0 } 13 invokestatic IoUtils.closeQuietly } 16 iconst_m1 17 ireturn 18 astore_2 19 aload_0 finally 20 invokestatic IoUtils.closeQuietly 23 aload_2 24 athrow
  • 95. 0 aload_0 Exception Handling 1 invokevirtual InputStream.read try / finally static int read( InputStream in ) { 4 istore_1 try { 5 aload_0 return in.read(); 6 invokestatic IoUtils.closeQuietly } catch ( IOException e ) { 9 iload_1 return -1; 10 ireturn } finally { 11 pop IoUtils.closeQuietly( in ); catch / finally 12 aload_0 } 13 invokestatic IoUtils.closeQuietly } 16 iconst_m1 17 ireturn 18 astore_2 19 aload_0 finally 20 invokestatic IoUtils.closeQuietly 23 aload_2 24 athrow
  • 96. 0 aload_0 Exception Handling 1 invokevirtual InputStream.read try / finally static int read( InputStream in ) { 4 istore_1 try { 5 aload_0 return in.read(); 6 invokestatic IoUtils.closeQuietly } catch ( IOException e ) { 9 iload_1 return -1; 10 ireturn } finally { 11 pop IoUtils.closeQuietly( in ); catch / finally 12 aload_0 } 13 invokestatic IoUtils.closeQuietly } 16 iconst_m1 17 ireturn Exception Table 18 astore_2 start end handler Exception 19 aload_0 0 5 11 IOException finally 20 invokestatic IoUtils.closeQuietly 0 5 18 any 23 aload_2 11 12 18 any 24 athrow
  • 97. 0 aload_0 Exception Handling 1 invokevirtual InputStream.read try / finally static int read( InputStream in ) { 4 istore_1 try { 5 aload_0 return in.read(); 6 invokestatic IoUtils.closeQuietly } catch ( IOException e ) { 9 iload_1 return -1; 10 ireturn } finally { 11 pop IoUtils.closeQuietly( in ); catch / finally 12 aload_0 } 13 invokestatic IoUtils.closeQuietly } 16 iconst_m1 17 ireturn Exception Table 18 astore_2 start end handler Exception 19 aload_0 0 5 11 IOException finally 20 invokestatic IoUtils.closeQuietly 0 5 18 any 23 aload_2 11 12 18 any 24 athrow
  • 98. More Attributes... Java 1.2+ InnerClass Synthetic Deprecated
  • 99. More Attributes... Java 1.2+ InnerClass Synthetic Deprecated Debugging LineNumberTable LocalVariableTable SourceFile
  • 100. More Attributes... Java 1.2+ Java 5+ InnerClass Signature Synthetic RuntimeVisibleAnnotations Deprecated RuntimeInvisibleAnnotations Debugging LineNumberTable LocalVariableTable SourceFile
  • 102. Resources The Java Virtual Machine Specification http://java.sun.com/docs/books/jvms/ JClassLib Viewer http://www.ej-technologies.com/products/jclasslib/ overview.html Maxine VM http://labs.oracle.com/projects/maxine/ New Features in Java 6 - Annotation Processing http://www.javabeat.net/articles/14-java-60-features- part-2-pluggable-annotation-proce-1.html