ChatGPT解决这个技术问题 Extra ChatGPT

What's the difference between compiled and interpreted language?

After reading some material on this subject I'm still not sure what the difference between a compiled language and an interpreted language is. I was told this is one of the differences between Java and JavaScript. Would someone please help me in understanding it?


N
Norman Ramsey

What’s the difference between compiled and interpreted language?

The difference is not in the language; it is in the implementation.

Having got that out of my system, here's an answer:

In a compiled implementation, the original program is translated into native machine instructions, which are executed directly by the hardware.

In an interpreted implementation, the original program is translated into something else. Another program, called "the interpreter", then examines "something else" and performs whatever actions are called for. Depending on the language and its implementation, there are a variety of forms of "something else". From more popular to less popular, "something else" might be Binary instructions for a virtual machine, often called bytecode, as is done in Lua, Python, Ruby, Smalltalk, and many other systems (the approach was popularized in the 1970s by the UCSD P-system and UCSD Pascal) A tree-like representation of the original program, such as an abstract-syntax tree, as is done for many prototype or educational interpreters A tokenized representation of the source program, similar to Tcl The characters of the source program, as was done in MINT and TRAC

Binary instructions for a virtual machine, often called bytecode, as is done in Lua, Python, Ruby, Smalltalk, and many other systems (the approach was popularized in the 1970s by the UCSD P-system and UCSD Pascal)

A tree-like representation of the original program, such as an abstract-syntax tree, as is done for many prototype or educational interpreters

A tokenized representation of the source program, similar to Tcl

The characters of the source program, as was done in MINT and TRAC

One thing that complicates the issue is that it is possible to translate (compile) bytecode into native machine instructions. Thus, a successful intepreted implementation might eventually acquire a compiler. If the compiler runs dynamically, behind the scenes, it is often called a just-in-time compiler or JIT compiler. JITs have been developed for Java, JavaScript, Lua, and I daresay many other languages. At that point you can have a hybrid implementation in which some code is interpreted and some code is compiled.


Sir, I have following questions 1. In which language is that "something else" written in? 2. And in the context of JavaScript, is that "something else" varied from browser to browser? 3. Say, my script is running in Google Chrome and Internet Explorer, is it interpreted same in both the browsers?
@Norman that was an awesome explanation. However, here's a couple of confusions that I still have. In a compiled implementation, the original program is converted into native machine instructions. How come? I thought C, for instance, after compilation would produce assembly code, which again anyway needs to be assembled by a native assembler into the underlying machine's machine code. So how's that different from a VM (python or JVM etc.) doing the same in case of an interpreted language?
C
Community

Java and JavaScript are a fairly bad example to demonstrate this difference, because both are interpreted languages. Java (interpreted) and C (or C++) (compiled) might have been a better example.

Why the striked-through text? As this answer correctly points out, interpreted/compiled is about a concrete implementation of a language, not about the language per se. While statements like "C is a compiled language" are generally true, there's nothing to stop someone from writing a C language interpreter. In fact, interpreters for C do exist.

Basically, compiled code can be executed directly by the computer's CPU. That is, the executable code is specified in the CPU's "native" language (assembly language).

The code of interpreted languages however must be translated at run-time from any format to CPU machine instructions. This translation is done by an interpreter.

Another way of putting it is that interpreted languages are code is translated to machine instructions step-by-step while the program is being executed, while compiled languages have code has been translated before program execution.


Java is interpreted? From wikipedia: "Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture."
@Personman that is still technically "interpreted" as the JVM is executing the code, not the OS itself. Its really a semantic difference anymore, as one could say that the complexity of modern OSes make the difference basically irrelevant for most situations. Your talking about the difference between the OS running the app, and the OS running an app which is running the code.
I suppose you mean that class files themselves are interpreted by the Java VM. That is kind of reasonable, but java source really is compiled to Java VM bytecode. You could build a physical Java Machine that would not require the VM to interpret it into another architecture's machine code. So it seems more accurate to say that Java is compiled. Still, this is a good example of the way in which the distinction is confusing and kind of arbitrary. After all, compiled C is interpreted by the CPU, right?
Java's a pretty bad example of either a compiled or interpreted language because it's essentially both. If I were going to make a comparison, I'd go with C and Lisp to avoid any confusion.
@stakx - actually Java bytecodes are normally compiled to native code by a JIT compiler as well. The only way to get pure interpreter behavior is to explicitly switch off the JIT compiler when the JVM is launched.
P
Pang

Here is the Basic Difference between Compiler vs Interpreter Language.

Compiler Language

Takes entire program as single input and converts it into object code which is stored in the file.

Intermediate Object code is generated

e.g: C,C++

Compiled programs run faster because compilation is done before execution.

Memory requirement is more due to the creation of object code.

Error are displayed after the entire program is compiled

Source code ---Compiler ---Machine Code ---Output

Interpreter Language:

Takes single instruction as single input and executes instructions.

Intermediate Object code is NOT generated

e.g: Perl, Python, Matlab

Interpreted programs run slower because compilation and execution take place simultaneously.

Memory requirement is less.

Error are displayed for every single instruction.

Source Code ---Interpreter ---Output


P
Pang

A compiler, in general, reads higher level language computer code and converts it to either p-code or native machine code. An interpreter runs directly from p-code or an interpreted code such as Basic or Lisp. Typically, compiled code runs much faster, is more compact, and has already found all of the syntax errors and many of the illegal reference errors. Interpreted code only finds such errors after the application attempts to interpret the affected code. Interpreted code is often good for simple applications that will only be used once or at most a couple times, or maybe even for prototyping. Compiled code is better for serious applications. A compiler first takes in the entire program, checks for errors, compiles it and then executes it. Whereas, an interpreter does this line by line, so it takes one line, checks it for errors, and then executes it.

If you need more information, just Google for "difference between compiler and interpreter".


Umm, not sure where you got some of this beyond the first two statements. This was technically true several generations ago with many interpreted languages, but depending on the platform and the attention to detail it is possible to have interpreted code that performs close to or as well as compiled code for certain activities.
Keeping in mind that languages like Java, C# and JavaScript which are almost flooding the entire programming world today, it will be unfair to say that "Compiled code is better for serious applications."
P
Personman

It is a very murky distinction, and in fact generally not a property of a language itself, but rather of the program you are using to execute code in that language.

However, most languages are used primarily in one form or the other, and yes, Java is essentially always compiled, while javascript is essentially always interpreted.

To compile source code is to run a program on it that generates a binary, executable file that, when run, has the behavior defined by the source. For instance, javac compiles human-readbale .java files into machine-readable .class files.

To interpret source code is run a program on it that produces the defined behavior right away, without generating an intermediary file. For instance, when your web browser loads stackoverflow.com, it interprets a bunch of javascript (which you can look at by viewing the page source) and produces lots of the nice effects these pages have - for instance, upvoting, or the little notifier bars across the top.


While Java first translates to bytecode and only while executing the JVM converts it to machine code; is it correct to say it is compiled and not interpreted?
I guess you can say that Java bytecode is interpreted, sure, but nobody writes Java bytecode. Java itself is usually compiled to bytecode.
Not only can you say it, it can be interpreted. And it can also be JIT compiled to machine code.
It seems like you think you're disagreeing with me about something, but I can't tell what.
P
Praveen Kishor

Interpreted language is executed at the run time according to the instructions like in shell scripting and compiled language is one which is compiled (changed into Assembly language, which CPU can understand ) and then executed like in c++.


M
MrIo

As other have said, compiled and interpreted are specific to an implementation of a programming language; they are not inherent in the language. For example, there are C interpreters.

However, we can (and in practice we do) classify programming languages based on its most common (sometimes canonical) implementation. For example, we say C is compiled.

First, we must define without ambiguity interpreters and compilers:

An interpreter for language X is a program (or a machine, or just some kind of mechanism in general) that executes any program p written in language X such that it performs the effects and evaluates the results as prescribed by the specification of X.

A compiler from X to Y is a program (or a machine, or just some kind of mechanism in general) that translates any program p from some language X into a semantically equivalent program p' in some language Y in such a way that interpreting p' with an interpreter for Y will yield the same results and have the same effects as interpreting p with an interpreter for X.

Notice that from a programmer point of view, CPUs are machine interpreters for their respective native machine language.

Now, we can do a tentative classification of programming languages into 3 categories depending on its most common implementation:

Hard Compiled languages: When the programs are compiled entirely to machine language. The only interpreter used is a CPU. Example: Usually, to run a program in C, the source code is compiled to machine language, which is then executed by a CPU.

Interpreted languages: When there is no compilation of any part of the original program to machine language. In other words, no new machine code is generated; only existing machine code is executed. An interpreter other than the CPU must also be used (usually a program).Example: In the canonical implementation of Python, the source code is compiled first to Python bytecode and then that bytecode is executed by CPython, an interpreter program for Python bytecode.

Soft Compiled languages: When an interpreter other than the CPU is used but also parts of the original program may be compiled to machine language. This is the case of Java, where the source code is compiled to bytecode first and then, the bytecode may be interpreted by the Java Interpreter and/or further compiled by the JIT compiler.

Sometimes, soft and hard compiled languages are refered to simply compiled, thus C#, Java, C, C++ are said to be compiled.

Within this categorization, JavaScript used to be an interpreted language, but that was many years ago. Nowadays, it is JIT-compiled to native machine language in most major JavaScript implementations so I would say that it falls into soft compiled languages.