• java字节码文件 helloworld


    Java代码

    \A.java
    public class A{}
    • 1
    • 2
    • 1
    • 2

    javac A.java \得到 A.class 
    javap -v A.class

    下面是javap工具帮我们生成的文本的字节码文件

    Classfile /home/yao/test/javap/A.class
      Last modified Aug 30, 2016; size 176 bytes
      MD5 checksum f35fe1f44d02cd491e6561a30327ceb5
      Compiled from "A.java"
    public class A
      minor version: 0
      major version: 52
      flags: ACC_PUBLIC, ACC_SUPER
    Constant pool:
       #1 = Methodref          #3.#10         // java/lang/Object."<init>":()V
       #2 = Class              #11            // A
       #3 = Class              #12            // java/lang/Object
       #4 = Utf8               <init>
       #5 = Utf8               ()V
       #6 = Utf8               Code
       #7 = Utf8               LineNumberTable
       #8 = Utf8               SourceFile
       #9 = Utf8               A.java
      #10 = NameAndType        #4:#5          // "<init>":()V
      #11 = Utf8               A
      #12 = Utf8               java/lang/Object
    {
      public A();
        descriptor: ()V
        flags: ACC_PUBLIC
        Code:
          stack=1, locals=1, args_size=1
             0: aload_0
             1: invokespecial #1                  // Method java/lang/Object."<init>":()V
             4: return
          LineNumberTable:
            line 1: 0
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    不过瘾?自己按照java规范翻译一遍~

    \通过linux hexdump -C A.class 可以以16进制方式查看文件
    00000000  ca fe ba be 00 00 00 34  00 0d 0a 00 03 00 0a 07  |.......4........|
    00000010  00 0b 07 00 0c 01 00 06  3c 69 6e 69 74 3e 01 00  |........<init>..|
    00000020  03 28 29 56 01 00 04 43  6f 64 65 01 00 0f 4c 69  |.()V...Code...Li|
    00000030  6e 65 4e 75 6d 62 65 72  54 61 62 6c 65 01 00 0a  |neNumberTable...|
    00000040  53 6f 75 72 63 65 46 69  6c 65 01 00 06 41 2e 6a  |SourceFile...A.j|
    00000050  61 76 61 0c 00 04 00 05  01 00 01 41 01 00 10 6a  |ava........A...j|
    00000060  61 76 61 2f 6c 61 6e 67  2f 4f 62 6a 65 63 74 00  |ava/lang/Object.|
    00000070  21 00 02 00 03 00 00 00  00 00 01 00 01 00 04 00  |!...............|
    00000080  05 00 01 00 06 00 00 00  1d 00 01 00 01 00 00 00  |................|
    00000090  05 2a b7 00 01 b1 00 00  00 01 00 07 00 00 00 06  |.*..............|
    000000a0  00 01 00 00 00 01 00 01  00 08 00 00 00 02 00 09  |................|
    000000b0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    下面就按java规范来翻译一下, 
    java官方字节码规范 https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24

    ->ca fe ba be
        magic u4
    ->00 00 00 34   
        minor_verion u2;major_version u2,jkd8
    ->00 0d ->13
        constant_pool_count
    ->#1 0a 00 03 00 0a    
        [CONSTANT_METHOD] u1 tag; u2 class_index; u2 name_and_type_index
    ->#2 07 00 0b 
        [CONSTANT_CLASS]  u1 tag; u2 name_index
    ->#3 07 00 0c
        [CONSTANT_CLASS]  u1 tag; u2 name_index
    ->#4 01 00 06 3c 69 6e 69 74 3e
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  <init>
    ->#5 01 00 03 28 29 56
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  ()V
    ->#6 01 00 04 43 6f 64 65
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  Code
    ->#7 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72  54 61 62 6c 65
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  LineNumberTable
    ->#8 01 00 0a 53 6f 75 72 63 65 46 69  6c 65
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  SourceFile
    ->#9 01 00 06 41 2e 6a 61 76 61
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  A.java
    ->#10 0c 00 04 00 05
        [CONSTANT_NAEMANDTYPE] u1 tag;u2 name_index;u2 descriptor_index;  00 04-><init> 00 05>()V
    ->#11 01 00 01 41
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  A
    ->#12 01 00 10 6a 61 76 61 2f 6c 61 6e 67  2f 4f 62 6a 65 63 74
        [CONSTANT_Utf8]   u1 tag; u2 length; u1[length] literal  java/lange/Object
    ->00 21 
        access flag u2, ACC_PUBLIC,ACC_SUPPER
    ->00 02
        u2 this_class
    ->00 03 
        u2 super_class
    ->00 00 
        u2 interfaces count
    ->00 00 
        u2 fields count 
    ->00 01 
        u2 methods count
    { method info #1
    ->00 01
        u2 access flag  public
    ->00 04
        u2 name index <init>
    ->00 05
        u2 descriptor index ()V
    ->00 01
        u2 attributes_count
    ->00 06
        u2 attribute_name_index  Code
    ->00 00 00 1d
        u4 attribute_length
    ->00 01
        u2 max_stack
    ->00 01
        u2 max_locals
    ->00 00 00 05
        u4 code_length
    ->2a b7 00 01 b1  
        u1 code[length]
    ->00 00
        u2 exception_table_length; 
    ->00 01 
        u2 attributes_count
    ->00 07 
        u2 attributes_name ->  LineNumberTable
    ->00 00 00 06 
        u4 attributes len
    ->00 01 
        u2 line_number_table_length
    ->00 00
        u2 start pc
    ->00 01 
        u2 linue number
    }   
    ->00 01
        u2 attributes count
    ->00 08
        u2 attribute_name_index  > SourceFIle
    ->00 00 00 02
        u4 attribute_length
    ->00 09
        u1 info[attribute_length] 00 09 -> #9 A.java 
  • 相关阅读:
    这些例子很炫,感兴趣的童鞋可以了解一下
    Java经典习题24
    Java经典习题23
    Java经典练习题22
    Java经典习题21
    Java经典习题20
    Java经典练习题19
    Java经典练习题18
    Java经典习题17
    Java经典习题16
  • 原文地址:https://www.cnblogs.com/fiyxiang/p/7308497.html
Copyright © 2020-2023  润新知