简体   繁体   English

编译器如何引用自己的输出

[英]How can the compiler reference its own output

"foreach" is a keyword of Java language and when compiler encounters this keyword in the source, it knows that the collection following this keyword must implement "iterable" interface. “foreach”是Java语言的一个关键字,当编译器在源码中遇到这个关键字时,就知道这个关键字后面的集合必须实现“iterable”接口。 The "iterable" interface itself is a piece of compiled Java code so how can the compiler reference its own output, how can the compiler be dependent on its own output? “可迭代”接口本身是一段编译后的Java代码,那么编译器如何引用自己的输出,编译器如何依赖自己的输出?

Think of a hypothetical point in past time when there is only Java compiler (probably written with C or C++) and Java framework is not compiled yet.想想过去的一个假设点,当时只有 Java 编译器(可能是用 C 或 C++ 编写的)而 Java 框架尚未编译。 Java.lang package and all the stuff in it is not compiled yet so there is no Iterable interface at that moment in time. Java.lang 包及其中的所有内容尚未编译,因此当时没有 Iterable 接口。 The Java compiler will compile the source code for Java.lang package from source code and build the bytecode for iterable interface in the future. Java 编译器将从源代码中编译 Java.lang 包的源代码,并在将来构建可迭代接口的字节码。 At that point in time somehow Java compiler has to know about the iterable interface (since it is part of the language specification) so it can compile source code that uses foreach iterator.在那个时候,Java 编译器必须以某种方式知道可迭代接口(因为它是语言规范的一部分),以便它可以编译使用 foreach 迭代器的源代码。 How is this made possible?这是如何实现的?

First of all , " foreach " is not a java keyword.首先,“ foreach ”不是java关键字。 Search for " extended for loop in java ".搜索“ extended for loop in java ”。 The second thing , Interfaces in java are just to abstract the real implementation.第二件事,java中的接口只是为了抽象真正的实现。 This is not must to implement Interface when we have the real Implemention Class.当我们有真正的实现类时,这不是实现接口所必须的。 But, to follow the good design principles like SOLID principles, we make Interfaces to abstract real implementations first, and also for multiple class hierarchies in java.但是,为了遵循 SOLID 原则等良好的设计原则,我们首先将接口抽象为真正的实现,同时也为 java 中的多个类层次结构。 Keywords do not implement Interfaces.If java keyword implement and refer any java code then its definitely related to bootstrapping means Java Compiler Written in Java Language.关键字不实现接口。如果 java 关键字实现并引用任何 Java 代码,那么它肯定与引导程序相关,即用 Java 语言编写的 Java 编译器。 Overall answer to your long question is you need to Study about Compiler Construction, Building JVM Language, Bootstrapping etc.您的长问题的总体答案是您需要研究编译器构建、构建 JVM 语言、引导程序等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM