What’s the difference between a compiled and interpreted language?

Interpretation and compilation are properties of the implementation of a language

It’s not accurate to say that a language is interpreted or compiled because interpretation and compilation are both properties of the implementation of that particular language, and not a property of the language itself. So, in theory, any language can be compiled or interpreted – it just depends on what the particular implementation that you are using does.

What exactly is compilation?

In a compiled implementation of a language, a compiler will translate the program directly into code that is specific to the target machine, which is also known as machine code – basically code that is specific to a given processor and operating system. Then the computer will run the machine code on its own.

What exactly is interpretation?

In an interpreted implementation of a language, the source code is not directly run by the target machine. What happens instead is that another program reads and then executes the original source code. This other program is also known as the interpreter. The interpreter is usually written specifically for the native machine. As an example, you could consider the multiplier operation – the “*”. If the interpreter sees this in your code, then at run time it would call its own definition of the multiplier function – maybe something called “multiply(x,y)”. And then that “multiply(x,y)” would execute the machine code’s equivalent of the multiply instruction.

In interpretation, the original source code is also typically converted into some intermediate code which is then processed by an interpreter that converts the intermediate code into machine specific code.

Are interpreters and compilers mutually exclusive?

No, they are not mutually exclusive – because there are some interpreters which also do some translation work, just like compilers normally do. So, the work that a compiler typically does can also be done by an interpreter.

Can a compiled implementation of a language do the same things as an interpreted implementation of a language?

Yes, you can accomplish exactly the same things with both interpreted and compiled languages. But, in general terms there are advantages and disadvantages of both compiled and interpreted languages.

So, what are the advantages and disadvantages of compilation and interpretation?

Because comparing compilation and interpretation is so dependent on the specific implementation of the interpreter and compiler, we can only compare compilation and interpretation in very general terms – there will be exceptions to what we say below, but in general these things are true:

  • Compiled implementations of languages have the advantage of being faster because they translate directly to the native code of the specific machine.
  • Interpreted implementations of languages tend to be more portable as well.
  • Interpreted implementations of languages are generally easier to create because writing compilers is difficult.

Is Java interpreted or compiled?

Interestingly enough, some implementations of Java are implemented as both interpreted and compiled – all at once. This is actually dependent on the JVM – Java is usually compiled down to the byte code by the Java compiler. But whether the JVM interprets or compiles that byte code is dependent on which JVM is being used. The difference between a JVM that compiles the byte code and a JVM that interprets the byte code is that an interpreting JVM will both translate the byte code and execute it at the same time as well, but with a JVM compiler the translation only occurs once, because an executable file is created in compilation. And that is the key difference between the two – although nowadays with just in time compilation the distinction is becoming a bit blurred.

What is the benefit of having Java be both interpreted and compiled?

When Java code is compiled from a .java file to a .class file, that .class file is what’s known as Java bytecode. Java is hardware/machine independent, which means that any machine can run Java bytecode provided it has a JVM specifically built for that machine. The bytecode is then interpreted (some JVM’s will compile the bytecode) by the given virtual machine. This means that the details of making the code work on a specific hardware platform are handled by the virtual machine.

Java is compiled to bytecode first because it allows the virtual machine to be more efficient because it only has to recognize bytecode. This is more efficient than just having the virtual machine interpret the original source code at run time.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

One thought on “What’s the difference between a compiled and an interpreted language?”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.