Java - Biên dịch - Thông dịch

Vừa rồi có đi phỏng vấn, một bác chắc làm cũng được 10 năm, hỏi Java là thông dịch hay biên dịch. Mình trả lời là biên dịch, bác ấy trơn mắt nhìn mình hỏi lại lần nữa. Vẫn trả lời là biện dịch vì tin rằng mình trả lời đúng.
Mặc dù biết khi chuyển sang bytecode JVM sẽ thông dịch từng dòng. anh em nào trên này biết câu trả lời chính xác không.

  • Java là ngôn ngữ biên dịch
  • Java vừa biện dịch vừa thông dịch
    Câu trả lời nào chính xác.
1 Like

Mình nghĩ là vừa biên dịch và vừa thông dịch.
javac.exe có nhiệm vụ biên dịch code java sang byteccode
Sau đó java.exe có nhiệm vụ thông dịch bytecode trong class file sang mã máy tùy vào chip/hệ điều hành.

5 Likes

Java là ngôn ngữ vừa biên dịch vừa thông dịch. Bạn trả lời biên dịch vẫn đúng.

Theo mình là cái ông đó muốn làm khó bạn. Bạn đi chỗ khác xin việc thì tốt hơn.

Với câu hỏi này thì bạn phải trả lời là : Compile once and run everywhere (both)

Javac is Java Compiler – Compiles your Java code into Bytecode
JVM is Java Virtual Machine – Runs/ Interprets/ translates Bytecode into Native Machine Code -> cái cuối dùng JIT

3 bước chính
.java —JAVAC (Java compiler) - >.class— JVM[ Interpreter(Run time code )line by line + JIT(Just in time-Complier)block] ----> Mã máy

Bonus:

An interpreter takes instructions one-by-one and calls some function that represents this instruction in native code (that’s what is mean by interpreting). It will do this over and over, even though finding the right function call for an instruction may be quite slow.

A JIT compiler takes an instruction and translates it into native code, then runs that native code. This may take a bit longer than interpretation on the first go, but as soon as the code is called a second time, it no longer needs to be translated, but runs natively and much faster.

2 Likes

Một câu trả lời trên SOF:

Java is compiled to an intermediate “byte code” at compilation time. This is in contrast to a language like C that is compiled to machine language at compilation time. The Java byte code cannot be directly executed on hardware the way that compiled C code can. Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be executed. The primary drawback of a language like C is that when it is compiled, that binary file will only work on one particular architecture (e.g. x86).

Interpreted languages like PHP are effectively system independent and rely on a system and architecture specific interpreter. This leads to much greater portability (the same PHP scripts work on Windows machines and Linux machines, etc.). However, this interpretation leads to a significant performance decrease. High-level languages like PHP require more time to interpret than machine-specific instructions that can be executed by the hardware.

Java seeks to find a compromise between a purely compiled language (with no portability) and a purely interpreted language (that is significantly slower). It accomplishes this by compiling the code into a form that is closer to machine language (actually, Java byte code is a machine language, simply for the Java Virtual Machine), but can still be easily transported between architectures. Because Java still requires a software layer for execution (the JVM) it is an interpreted language. However, the interpreter (the JVM) operates on an intermediate form known as byte code rather than on the raw source files. This byte code is generated at compile time by the Java compiler. Therefore, Java is also a compiled language. By operating this way, Java gets some of the benefits of compiled languages, while also getting some of the benefits of interpreted languages. However, it also inherits some limitations from both of these languages.

As Bozho points out, there are some strategies for increasing the performance of Java code (and other byte code languages like .Net) through the use of Just in Time (JIT) compilation. The actual process varies from implementation to implementation based on the requirements, but the end-result is that the original code is compiled into byte code at compile time, but then it is run through a compiler at runtime before it is executed. By doing this, the code can be executed at near-native speeds. Some platforms (I believe .Net does this) saves the result of the JIT compilation, replacing the byte code. By doing this, all future executions of the program will execute as though the program was natively compiled from the beginning.

2 Likes

Cái này trước đó có hỏi và đã giải thích rồi. mình vẫn giữ quan điểm nó là ngôn ngữ biên dịch :D, có điều vị trí apply là senior chắc yêu cầu phải giải thích rõ hơn :smiley:

Nếu là Senior thì mình nghĩ trả lời như vậy có thể không thích hợp. Bạn không nên nghĩ Java là compile hay interperter. Bởi vì mọi ngôn ngữ đều có thể interpreted and/or compiled. Chỉ có trường hợp nó sử dụng hay không thôi

1 Like

Java là ngôn ngữ biên dịch, làm gì có ngôn ngữ nào vừa biên dịch vừa thông dịch đâu.

Nếu bạn không biết rõ thì không nên nói bừa tránh làm người khác hoang mang.

From Joe: A developer of Sun

The term compiler and interpreter both are valid. They depend on the viewpoint where you stand and view the object. Insider or outsider.
Compiler: from the standpoint of JVM JVM together with ,java is a compiler, because the generated byte-codes are executed directly by JVM (insider).

Interpreter: from the viewpoint of JVM’s guest Operating System (Windows or Linux) JVM is an interpreter because it interprets the byte-codes and run an according subprogram that executes the work (outsider).

JIT-Compiler of JVM: Some works (e.g. IO a file, ++ or --, etc.) can be “foreseen” and therefore can be “compiled” into the guest-native codes in order to enhance the performance. For the guest OS and JVM JIT is a real compiler.

Đây là câu hỏi bẫy. Cả hai mới là đúng.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?