0

I would like to understand if Java programs are Interpreted or compiled, and why?

I (think I) know the definitions of compilation and intrepertations:

Compilation - Translates the source code to machine code. Then the machine code can be executed.

Interpreted - Translates the code and runs it at the same time. Translates one line of source code, runs it, translates the next line, runs it, and so on.

Are these definitions correct? If so, are Java programs Interpreted or compiled, and why?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 3
    Those are two possible definitions of compilation and interpretation. Neither applies to Java, or almost any other modern PL implementation. – Fred Foo Mar 03 '14 at 20:24
  • Try searching with your exact title: https://www.google.com/search?q=Java+-+Interpreted+or+compiled%3F – aliteralmind Mar 03 '14 at 20:29

1 Answers1

2

Java is neither and both.

Java sourcecode is compiled to bytecode, which is then interpreted by the Java Virtual Machine.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • Although may also be JITed from bytecode to machine code if needed(rather than interpreting the bytecode) – Richard Tingle Mar 03 '14 at 20:25
  • @Philipp Thanks for your answer. I understand the *'sourcecode is compiled to bytecode'* part. But I'm having trouble understanding the *'bytecode is interpreted by the Java Virtual Machine'* part. The definition of interpretation (correct me if I'm wrong) is roughly: Translate a line of code (in this case, the bytecode) to a more low-level language, and then run this line (and so on). If this is true, **then what more low-level language does the interpretation convert the bytecode to?** Is it pure machine level binary code? – Aviv Cohn Mar 03 '14 at 20:32
  • @Prog Yes. Java bytecode is kind of a machine-independent assembler-code. The JVM then translates it to instructions for the CPU it is executed on. (grossly oversimplified) – Philipp Mar 03 '14 at 20:43
  • @Philipp Weird, I always thought Java has nothing to do with binary code specific to the CPU it runs on. Always thought it runs entirely on a virtual machine. So that means that this isn't entirely true? Is the code that the interpretation generates, pure physical-machine-specific binary code, which gets run by the physical computer itself, like when compiling C++ code? (Just want to make sure). – Aviv Cohn Mar 03 '14 at 21:42