0
votes

Wikipedia and some other sites describe interpreters as translators that translates code from some high-level language to some low-level language. However, there is alot of explanations out there, including in stackoverflow, where it says the interpreter directly executes the instruction its taken as input without prior conversion. So is interpreter a translator like compilers or an executor like CPU?

2
"Wikipedia and some other sites describe interpreters as translators that translates code from some high-level language to some low-level language." Where does Wikipedia say that?sepp2k
Your assumption is wrong (or at least too simplistic): the java executable is not an interpreter of the Java language, it is a virtual machine that processes bytecode, which is essentially machinecode for that virtual machine.Mark Rotteveel
Is taking bytecode and running the native code to do the same thing not conversion on the fly?Peter Lawrey

2 Answers

2
votes

That very much depends.

You have to understand that current day jvm implementations are big, powerful tools.

Typically, they have an interpreter part. That one kicks in first: it reads byte code and directly interprets that itself. In the direct sense of the its name Java virtual machine!

But as soon as specific code gets executed repeatedly (many thousands of repetitions that is), the so called just in time compilers kick in. They translate the byte code into native machine code and do all kinds of performance optimizations, such as method inlining.

Such jvms are thus doing both things you mentioned in your question.

1
votes

When you compile the Java code, you turn it into Java byte-code. When you execute your Java program, you actually invoke the Java Virtual Machine to interpret the Java byte-code. The JVM interprets the Java byte-code, and generates platform-specific byte-code from it, that can then be executed by the platform.

That's the simplified explanation of what it does, but it does a lot more: like @GhostCat said, among other things, it sometimes compiles blocks of code that are often executed, and keeps the compiled version (in a form of platform-specific byte-code), to avoid interpreting it every time (this is called JIT compilation).

This makes Java a hybrid between compilation and interpretation: the source code is compiled to Java byte-code, but then that Java byte-code is interpreted on a specific platform.