英文:
Where are the implementation details of Java located within the JDK?
问题
抱歉,我只能返回英文翻译。以下是您提供的内容的翻译:
我已经向我的同行提问,得到了各种不同的答案,这促使我在这里提问。如果这是一个非常简单的问题,我感到抱歉,但在Java中,实现细节或内置类位于何处?我听说编译器将保存Java的实现细节。然而,我也听说JRE将拥有Java的类或实现细节,特别是rt.jar文件。最初,我认为Java标准库会包含运行Java并能够编写Java代码所需的所有细节,因为它带有java.lang包,其中包含对Java编程语言设计至关重要的类。对于造成的困惑,我感到抱歉,但是否有一个明确的答案呢?是否有可能这些内容会由多个不同的事物保存呢?
英文:
I have asked my peers and I have found various different answers, so which has led me to ask here. I apologize if this is a very simple question, but where are the implementation details or built in classes located at in Java? I have heard that the compiler will hold the implementation details of Java. However, I have also heard how the JRE will have the classes or details of the implementation for Java, specifically the rt.jar. I originally thought that the java standard library would have all the details to run java and be able to write java code since it comes with the java.lang package, which carries classes that are fundamental to the design of the Java programming language. Sorry for the confusion, but is there a clear answer to this. Is it possible that multiple of these things hold such info.?
答案1
得分: 1
> 在Java中,实现细节或内置类位于何处?
实现细节是... 一切。整个Java JRE或JDK安装都是实现细节。你可以说... 在OpenJDK源代码树中的一切都是实现细节。
组成Java SE类库的内置(Java)类根据Java版本而异地位于不同位置。
-
对于Java 8及更早版本,Java SE类库的编译类位于“rt.jar”文件中。其他类(例如JDK工具)位于其他JAR文件中。
-
对于Java 9及更高版本,编译类存储在“jmods”目录中。这些不再是JAR文件。
注意,“rt.jar”(加上其他JAR文件)和“jmods”目录并不是完整的实现:
-
一些Java SE类具有在本地(C或C++)代码中实现的“native”方法。类加载、线程和低级I/O支持就是其中的例子。
-
大部分Java运行时实现与任何Java类都没有直接关系。例如,字节码解释器、JIT编译器、垃圾回收器以及各种代理和监控钩子都是用C / C++实现的,通常是主要的“java”可执行文件的一部分。
> 我听说编译器会保存Java的实现细节。
那是不正确的。编译器对“java.lang”包中一些类的签名具有少量内置知识。然而,在大多数情况下,Java编译器会从“rt.jar”、 “jmods”或其他地方加载“.class”文件。
> 我最初认为Java标准库会包含运行Java所需的所有细节,并能够编写Java代码,因为它带有java.lang
包,其中包含了对Java编程语言设计至关重要的类。
这绝对不是真的。
除此之外,你混淆了很多东西。首先,你混淆了设计和实现。它们是不同的东西。真的。
- 设计是文件:规范、javadoc等等。
- 实现是代码,用至少3种编程语言编写。
其次,你混淆了Java 语言的设计与Java 运行时系统的设计。实际上,设计有很多部分:
- Java语言...在JLS中规定。
- Java虚拟机...在JVMS中规定。
- Java SE类库...在Java SE javadocs中规定。
- 设计的其他各个方面...在其他文档中规定。
值得注意的是,各自的规范在很大程度上是可分割的:
- 你可以在没有JVM规范的情况下实现Java语言;例如,Android使用了Dalvik,然后是ART。
- 你可以在JVM规范之上实现其他编程语言;例如,通过编写一个生成JVM字节码的编译器。
- 你可以实现具有与Java SE库没有任何共同之处的类库的Java语言。
最后,如上所述,JLS和JVMS的大部分实现都在本机代码中,而且(在较小程度上)是Java类,它们并不正式地<sup>1</sup>属于Java SE类库的一部分。
> 是否可以说java.lang
基本上提供了Java语言的基本框架?
不是的。请参见上文。Java语言的设计(其语法和语义)在JLS中。实现(将设计映射为实际工作内容)包括Java字节码编译器、字节码解释器、JIT编译器等等。
如果你真的想理解这一切是如何工作的,你应该从下载OpenJDK源代码树开始。所有的代码都在那里...
<sup>1 - 我在这里谈论的是“内部”类,以及(在Java 8及更早版本中)包含在“tools.jar”中的类。例如,javac
字节码编译器的Java源代码。</sup>
英文:
> Where are the implementation details or built in classes located at in Java?
The implementation details are ... everything. The entire Java JRE or JDK installation is implementation details. You could say ... everything in the OpenJDK source tree is implementation details.
The builtin (Java) classes that comprise the Java SE class libraries are in different places depending on the Java version.
-
For Java 8 and earlier, the compiled classes for the Java SE class libraries are in the "rt.jar" file. Additional classes (for example the JDK tools) are in other JAR files.
-
For Java 9 and later, the compiled classes are stored in the "jmods" directory. These are no longer JAR files.
Note that "rt.jar" (+ other JARs) and the "jmods" directory are not the complete implementation:
-
Some Java SE classes have
native
methods which are implemented in native (C or C++) code. Classloading, threads and low-level I/O support are examples of this. -
Much of the Java runtime implementation does not have any direct relation with any Java classes. For example, the bytecode interpreter, the JIT compiler and the garbage collector, along with the various agent and monitoring hooks are implemented in C / C++ and (typically) part of the main
java
executable.
> I have heard that the compiler will hold the implementation details of Java.
That is not correct. The compiler has a small amount of built-in knowledge of the signatures for some classes in the java.lang
package. However, in most cases a Java compiler loads ".class" files from "rt.jar" or "jmods" or wherever.
> I originally thought that the java standard library would have all the details to run java and be able to write java code since it comes with the java.lang
package, which carries classes that are fundamental to the design of the Java programming language.
That is certainly not true.
Besides you are conflating lots of things. Firstly, you are conflating design and implementation. They are different things. Really.
- The design is documents: specifications, javadocs and so on.
- The implementation is code, written in (at least) 3 programming languages.
Secondly, you are conflating the design of the Java language with the design of the Java runtime system. In reality, the design has many parts:
- The Java language ... specified in the JLS.
- The Java Virtual Machine ... specified in the JVMS.
- The Java SE class libraries ... specified in the Java SE javadocs.
- Various other aspects of the design ... specified in other documents.
It is worth noting that the respective specifications are separable to a significant degree:
- You can implement the Java language without the JVM spec; e.g. Android used Dalvik and then ART.
- You can implement other programming languages on top of the JVM spec; e.g. by writing a compiler that emits JVM bytecodes.
- You could implement the Java language with class libraries that have nothing in common with the Java SE libraries.
Finally, as explained above, most of the implementation of the JLS and JVMS is in native code and (to a lesser extent) Java classes that are not formally<sup>1</sup> part of the Java SE class library.
> Would it be safe to say that the java.lang
basically provides the barebones of the java language?
No. See above. The design of the Java language (its syntax and semantics) are in the JLS. The implementation (which maps the design to something that works) comprises the Java bytecode compiler, bytecode interpreter, JIT compiler and so on.
If you really want to understand how this all works, you should start by downloading the OpenJDK source tree. All of the code is in there ...
<sup>1 - I am talking here about "internal" classes, and classes that (in Java 8 and earlier) were part of "tools.jar". For example, the Java source code for the javac
bytecode compiler.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论