java *.java 和 java *.class 之间的区别是什么?

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

What is the difference between java *.java and java *.class

问题

以下是您要翻译的内容:

class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

当我编译Main.java源代码时,它会创建Main.class文件,然后我了解了JVM的工作过程,但我刚刚知道我可以在不编译的情况下执行Main源代码,而且当我执行java Main.java时,需要更多时间,为什么它不会在执行java Main.java时创建Main.class文件,以及如何在不创建Main.class文件的情况下执行它是如何可能的?

我预期不会产生结果并且不会发生错误,但事实并非如此。

英文:

Hello Here is very simple code and I'm curious about the difference between 'java Main.java and Main.class

class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

when I compile the Main.java source it creates the Main.class and then I know the proccess of the JVM Work but I just got to know That I can execute Main source without compiling

and it takes a little bit more time when I execute java Main.java

so why it doesn't create Main.class when I execute java Main.java and how it is possible to execute without creating Main.class file?

I expected no Result and error occurs but not..

答案1

得分: 0

.java 文件是源文件,而 .class 文件是由 javac 生成的字节码 - Java 编译器。

通过执行以下命令:

javac HelloWorld.java

编译器会生成字节码。

一般来说,你不会在没有编译的情况下运行 .java 文件。如果你是从一个集成开发环境(IDE)运行项目,那么 IDE 可能 会编译你的代码然后执行它。这是一个两步的过程,代码首先被编译然后才被执行。如果这些 .class 编译文件是由 IDE 生成的,你通常看不到它们,因为它们 通常 位于一个临时的隐藏目录中,而不是项目目录中。

英文:

The .java files are source files, whereas .class files are the bytecode generated by javac - the Java Compiler.

By doing

javac HelloWorld.java

The compiler generates the bytecode.

In general, you do not run the .java files without compiling it. If you're running the project from a IDE, the IDE is probably compiling your code and then executing it. It's a two-step process where the code gets compiled and then executed. You do not see the .class compiled files if they were generated by the IDE, because they're typically located in a temporary hidden directory and not in the project directory.

答案2

得分: -2

当您在终端执行命令"java Main.java"时,它会尝试直接运行Java源代码文件,而不会首先明确编译它。负责执行Java程序的Java虚拟机(JVM)在某些情况下可以直接理解和解释Java源代码。

然而,在大多数情况下,JVM期望Java源代码在执行之前被编译成字节码。字节码存储在一个带有".class"扩展名的单独文件中,例如"Main.class"。这个字节码文件是通过Java编译器(javac)在使用命令"javac Main.java"编译Java源代码时生成的。

因此,当您执行"java Main.java"而不是明确编译它时,JVM会尝试直接解释源代码,但这可能不总是成功或产生预期的结果。通常建议使用"javac"编译Java源代码以生成字节码文件,然后使用"java"来执行字节码(例如"java Main")。

在您的情况下,由于您在执行"java Main.java"而不是"java Main"时遇到了延迟,这可能是因为JVM正在尝试在运行时动态编译源代码,这会增加执行过程的一些开销和额外时间。然而,这种行为可能会因您使用的Java实现和版本而有所不同。

英文:

When you execute the command "java Main.java" in the terminal, it attempts to directly run the Java source file without explicitly compiling it first. The Java Virtual Machine (JVM) responsible for executing Java programs can understand and interpret Java source code directly in certain cases.

However, in most cases, the JVM expects the Java source code to be compiled into bytecode before execution. The bytecode is stored in a separate file with a ".class" extension, such as "Main.class". This bytecode file is generated by the Java compiler (javac) when you compile the Java source code using the command "javac Main.java".

So, when you execute "java Main.java" without explicitly compiling it, the JVM tries to interpret the source code directly, but it may not always be successful or produce the expected results. It's generally recommended to compile the Java source code using "javac" to generate the bytecode file and then use "java" to execute the bytecode (e.g., "java Main").

In your case, since you're experiencing a delay when executing "java Main.java" instead of "java Main", it's possible that the JVM is attempting to dynamically compile the source code at runtime, which adds some overhead and additional time for the execution process. However, this behavior may vary depending on the Java implementation and version you're using.

huangapple
  • 本文由 发表于 2023年6月12日 20:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456782.html
匿名

发表评论

匿名网友

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

确定