Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java语言编译运行 #45

Open
HerryLo opened this issue Oct 25, 2023 · 0 comments
Open

Java语言编译运行 #45

HerryLo opened this issue Oct 25, 2023 · 0 comments
Assignees
Labels
Blog 文章

Comments

@HerryLo
Copy link
Member

HerryLo commented Oct 25, 2023

Java语言编译运行

java代码需要编译字节码,再通过虚拟机JVM编译成机器码才可运行:

编译时主要负责语法和类型检查,而运行时则负责实例化对象、执行方法等任务。

编译

javac HelloWorld.java

编译阶段主要完成以下几个工作:

  • 词法分析:对代码进行词法分析产生字符流;
  • 语法分析:编译器会对代码进行语法分析,检查代码是否符合语法规则;
  • 语义分析:编译器会进一步检查代码语义是否正确。比如,变量是否已经声明、类型是否匹配等;
  • 生成字节码:编译器会将代码转化为字节码,字节码包含了 Java 虚拟机指令集中的指令、常量池和其他相关信息;

运行

java HelloWorld

在运行时,该阶段主要完成了以下几个工作:

  • 类加载器:字节码文件经过类加载器到JVM内存;
  • 类解释器:字节码经过字节码解释器解释成系统可识别的指令码;
  • 执行阶段:系统再向硬件设备发送指令码执行操作。

参考

Java编译原理(javac)

如何编译和运行Java文件?

JVM基础(一)JVM的工作流程

@HerryLo HerryLo added the Blog 文章 label Oct 25, 2023
@HerryLo HerryLo self-assigned this Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blog 文章
Projects
None yet
Development

No branches or pull requests

1 participant