首页 > 其他分享 >JVM(一)--JVM介绍和Class File

JVM(一)--JVM介绍和Class File

时间:2022-11-24 23:33:47浏览次数:58  
标签:count constant JVM -- u2 File CONSTANT pool

什么是JVM

流程图(代表一次编译 到处运行)

Java虚拟机与物理机对比

Class文件类比输入设备

CPU指令集类比输出设备

JVM类比存储器、控制器、运算器等

JVM products

最常用的目前是HotSpot,可以通过java -version命令查看

Oracle:HotSpot、JRockit

IBM:J9 VM

Ali:TaobaoVM

Zual:Zing

JDK JRE JVM

官网:https://docs.oracle.com/javase/8/docs/index.html

结合JDK看JVM

(1)能够把Class文件翻译成不同平台的CPU指令集

(2)也是Write Once Run Anywhere的保证

HotSpot JVM Architecture

官网:https://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

Class file

Java Source

Java源码文件、Kotlin源码文件、Groovy源码文件等

public class User {

    private Integer age;
    private String name = "snail";
    private Double salary = 100.0;
    private static String address;

    public void say() {
        System.out.println("snail Say...");
    }

    public static Integer calc(Integer op1, Integer op2) {
        op1 = 3;
        Integer result = op1 + op2;
        return result;
    }

    public static void main(String[] args) {
        System.out.println(calc(1, 2));
    }
}

Early compile

javac User.java 编译成User.class

Class format

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html

  • 查看字节码文件

    使用jdk自带的 javap 指令 javap -v User.class > User.txt

  • 查看虚拟机解释执行的二进制字节码

    可以使用Notepad++ 安装一个HEX一Editor插件,就可以看到如下

class二进制字节码对照表

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}

Analyse

(1)cafebabe

The magic item supplies the magic number identifying the class file format

(2)0000 + 0034: minor_version + major_version

16进制的34等于10进制的52,表示JDK的版本为8

(3)0043:constant_pool_count

The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one
16进制的43等于10进制的67,表示常量池中常量的数量是66

(4)cp_info:constant_pool[constant_pool_count-1]

The constant_pool is a table of structures representing various string constants, class and interface names, field names, 
and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool 
table entry is indicated by its first "tag" byte.
The constant_pool table is indexed from 1 to constant_pool_count - 1.

字面量:文本字符串,final修饰的常量等
符号引用:类和接口的全限定名、字段名称和描述符、方法名称和描述符

(5)The constant pool

官网:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4

cp_info {
    u1 tag;
    u1 info[];
}

Table 4.4-A. Constant pool tags

Constant Type Value
CONSTANT_Class 7
CONSTANT_Fieldref 9
CONSTANT_Methodref 10
CONSTANT_InterfaceMethodref 11
CONSTANT_String 8
CONSTANT_Integer 3
CONSTANT_Float 4
CONSTANT_Long 5
CONSTANT_Double 6
CONSTANT_NameAndType 12
CONSTANT_Utf8 1
CONSTANT_MethodHandle 15
CONSTANT_MethodType 16
CONSTANT_InvokeDynamic 18

(6)First constant

由0a可以知道第一个常量的类型对应的10进制为10,所以这表示一个方法引用,查找方法引用对应的

structure

CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index; //代表的是class_index,表示该方法所属的类在常量池中的索引
    u2 name_and_type_index; //代表的是name_and_type_index,表示该方法的名称和类型的索引
}

经过分析可以得出第一个常量表示的形式为

#1 = Methodref  #16.#35 // java/lang/Object."<init>":()V

(7)Second constant

由08可以知道第一个常量的类型对应的10进制为8,所以这表示一个String引用,查找方法引用对应的

structure

CONSTANT_String_info {
    u1 tag;
    u2 string_index; //代表的是string_index
}

经过分析可以得出第1、2个常量表示的形式为

#1 = Methodref  #16.#35 // java/lang/Object."<init>":()V
#2 = String     #36     // snail

反汇编

JDK自带的命令 javap

JVM相对class文件来说可以理解为是操作系统;class文件相对JVM来说可以理解为是汇编语言或者机 器语言。

类加载机制

官网:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html

标签:count,constant,JVM,--,u2,File,CONSTANT,pool
From: https://www.cnblogs.com/snail-gao/p/16923841.html

相关文章

  • String 字符串常用的方法
    String字符串常用的方法 <body><script>//1.split(用于把字符串分割为数组)把字符串转换为数组和join()相反letstr="pink$red$blue";......
  • 一些简单的图论模型和建图技巧
    1.常见模型先有模型,而后有建图技巧.二分图和网络流那一套建图不是这篇文章讨论的内容.看上去全是最短路相关的建图(本来想写2-SAT的,但是那部分内容已经包含在S......
  • 通达信 缠论分笔、分段DLL插件使用说明
    1将TDXchan.dll文件复制到C:\zd_zsone\T0002\dlls。这是通达信股票软件的安装目录,根据你的当前环境调整。进入安装目录发现没有dlls文件夹时,可以自己创建。2绑定DL......
  • 【Leecode】有效括号
    【题目描述】给定一个只包括 ​​'('​​,​​')'​​,​​'{'​​,​​'}'​​,​​'['​​,​​']'​​ ​​s​​ ,判断字符串是否有效​有效字符串需满足:左括号必须用相同......
  • Vue -- Mixin混入(二)
    前言使用Mixin混入自定义属性自定义属性:就是直接写在组件里的属性定义一个Mixin,并写入自定义属性 constmyMixin={num:1};创建vue实例,定义......
  • JavaSpark 读取 HBASE
    1、pom.xml<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties>......
  • FOR XML PATH多行合并到一行
     FORXMLPATH有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的......
  • Objects.requireNonNull的意义是什么
    Objects.requireNonNull方法的源码是这样:publicstatic<T>TrequireNonNull(Tobj){if(obj==null)thrownewNullPointerException();returno......
  • ElasticSearch中查询(排序、高亮、分页、Filter过滤、_source筛选)
    排序(sort)sort可以让我们按照不同的字段进行排序,并且通过order指定排序的方式asc升序desc倒序GETmy_index/_search{"query":{"match":{"title":......
  • 数据库配置
    目录登录mysqlNavicat手动连接命令行连接创建一个库使用Navicat图形化操作命令行创建查看用户5.7之前版本5.7往后的版本创建用户一键建用户加赋权分布式建用户登录mysql......