首页 > 解决方案 > Java代码的正确执行顺序是什么

问题描述

我们在有关 java 代码执行的测试中遇到了这个问题:-

选择正确的java代码执行顺序

a) 类加载器

b) 解释

c) 编译

d) 字节码验证

e) Java 源代码

f) 执行

选项 :-

  1. eacbdf
  2. eabcdf
  3. ebacbf
  4. ecadbf

我将选项 4 标记为我的选择。我不知道确切的原因,但那个听起来对我来说最好。

结果,它说我的答案是错误的。T_T

有人能说出正确答案是什么以及为什么。

(大学声称的正确答案是选项 3)(在给出此帖子的链接后,大学更正了选项 4 的解决方案)

(看了大家的评论和解决方案后,对执行过程有了更深入的了解,而不是仅仅解决一个愚蠢的错误MCQ...... GREAT COMMUNITY)。

标签: javajvm

解决方案


正确的顺序是ecadbf,所以正确的答案是4

其他三个答案可能是错误的,因为这三个答案都在c) 之前有 a) 但你根本不能在c) 编译步骤之前有a) 类加载器,该步骤生成类加载器应该加载的字节码。

┌──────────────────────────────────┐
│ e) Java Source Code (.java file) │     Obvious starting point
└──────────────────────────────────┘
                 ↓
 ┌────────────────────────────────┐
 │ c) Compilation (javac command) │
 └────────────────────────────────┘      Then we compile the source code
                 ↓                       to byte code.
     ┌────────────────────────┐
     │ Bytecode (.class file) │
     └────────────────────────┘
                 ↓
       ┌─────────────────┐               The classloader is responsible for
┌──────┤ a) Class Loader ├─ JVM ┐        locating the byte code and making it
│      └─────────────────┘      │        available to the JVM as a byte array
│                ↓              │
│ ┌───────────────────────────┐ │        In the linking phase of preparing a
│ │ d) Byte Code Verification │ │        class for use, the byte code is
│ └───────────────────────────┘ │        verified by the JVM.
│                ↓              │
│     ┌───────────────────┐     │
│     │ b) Interpretation │     │        This one is a bit iffy, but you can
│     └───────────────────┘     │        argue that the byte code is interpreted
│                ↓              │        before it is executed, but the JVM
│        ┌──────────────┐       │        could easily JIT-compile to native
│        │ f) Execution │       │        code without ever executing the code
│        └──────────────┘       │        in interpretation mode.
└───────────────────────────────┘

请注意,ClassLoader稍后还会再次使用 来按需加载资源。


推荐阅读