Java编译器如何看待源代码?

huangapple go评论97阅读模式
英文:

How does the java compiler look at the source code?

问题

  1. Java编译器在实际运行程序之前知道什么?(在我们在集成开发环境中键入程序时会检查什么内容,与在运行时检查的内容有什么区别?)

例如A)据我所知,常量值会立即在集成开发环境中的源代码中进行评估(在我们键入它们时),而对于变量,编译器仅会检查变量是否可访问和/或变量的类型兼容性,但不会立即加载变量的实际值。这是在运行时完成的。
例如B)重载在编译时进行检查,但部分重写是在运行时进行检查的。


  1. 是否有其他类似的示例和/或编译器根据哪些标准/规则决定是否应在编译时或运行时检查特定类型的代码?

英文:
  1. What does the Java compiler know before we actually run the program? (What things are checked as we type a program in an IDE vs what are the things which are checked at runtime?)

e.g. A) As far as I know, constant values are evaluated immediately (as we type them) in the source code in an IDE whereas, in case of variables, the compiler only checks if the variable is reachable and/or type compatibility of the variable but does not load the actual value of the variable right then. It is done at run time.
e.g. B) Overloading is checked at the compile-time, but overriding is (partially) checked at runtime.


  1. Are there any other such examples and/or any criteria/rule by which the compiler decides if a particular type of code should be checked at compile time or run time?

I have tried going through a few books and Java docs as well, but I couldn't find a satisfactory answer.

答案1

得分: 2

没有集成开发环境(IDE):

  1. 使用编辑器,在一个或多个文件中编写源代码,假设你称一个文件为Foo.java。

  2. 编译成类文件:javac Foo.java

  3. 在Java虚拟机中运行生成的编译代码:java Foo

所以,编译器接收到源代码。

集成开发环境(IDE)是(泛泛地说)编辑器、编译器和Java虚拟机的综合界面。它可能还会对你键入的Java代码进行“即时”检查,以便更早地发现错误。作为IDE的一部分,它显然可以访问源代码。


对于修改后的问题:

对于所有生成错误的情况,我更倾向于不使用“throw”这个词,因为在Java异常处理方面,“throw”有着非常特定的意义。

编译器根据语言的定义分析你的源代码。如果源代码不是合法的Java代码,它会告诉你。它还可能针对代码中可疑的结构提供有用的警告。如果编译器能够告诉你,它会这样做。

但是,在程序运行之前,编译器无法了解某些事情(如果这不是真的,那么永远不需要运行程序)。这包括可能发生的错误。例如,如果你正在对用户输入的数据进行算术计算,特定的数据值可能会导致算术异常(例如除以零)。这些都是运行时错误,在编译器的责任范围之外。此时编译器并未运行。在编译代码和运行代码之间可能已经过去了多年的时间。

英文:

Without an IDE:

  1. Using an editor, you write source code in one or more files, assume you call a file Foo.java.

  2. You compile it to a class file: javac Foo.java.

  3. You run the resulting compiled code in a Java virtual machine: java Foo.

So, the compiler is given the source code.

An IDE is (loosely speaking) an integrated interface to the editor, compiler, and Java virtual machine. It may also do "on the fly" checking of the Java code you type, so you find errors earlier. Being part of the IDE, it obviously has access to the source code.


For the amended question:

I prefer not to use the word 'throw' for all cases of generating an error, since 'throw' has a very specific meaning with respect to Java exception handling.

The compiler analyzes your source code according to the definition of the language. If the source code is not legal Java, it will tell you. It may also offer useful warnings about questionable constructs in the code. If the compiler can tell you, it will.

But there are things the compiler cannot know about before the program is run (if that were not true, then there would be no point in ever running programs). This includes errors that might occur. For example, if you're doing arithmetic calculations with user-entered data, arithmetic exceptions (divide by zero, maybe) may happen with particular data values but not others. These are all run-time errors, outside any responsibility of the compiler. The compiler is not running at this point. Years may have elapsed between compiling the code and running it.

答案2

得分: 0

你提供的覆盖/重载示例有些令人困惑。编译器会对这两者都进行检查,因此如果使用不正确,你会在两种情况下都得到编译器错误。

区别在于对于重载的方法,编译器可以立即链接到实现,而对于重写的方法,编译器需要插入一小段运行时代码,通过检查运行时所属的子类来查找实现。

一般来说,编译器会尽量在编译时进行尽可能多的检查,但是检查的确切列表在不同版本的编译器和语言之间不断变化。

英文:

Your overriding/overloading example is a bit confusing. Those are both checked by the compiler, so if you're doing it wrong, you get compiler errors for both.

The difference is that for overloaded methods, the compiler can link to the implementation immediately, while for overridden methods, the compiler needs to insert a small piece of runtime code to look up the implementation by checking which subclass it has at runtime.

In general, the compiler tries to check as much as possible at compile-time, but the exact list of checks is constantly changing between different versions of the compiler and the language.

huangapple
  • 本文由 发表于 2020年8月15日 21:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定