views:

253

answers:

4

After reading some material on this subject I'm still not sure. I was told this is one of the differences between java and javascript. Would someone please help me in understanding it?

Thanks, Mike

+5  A: 

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

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 translated to machine instructions step-by-step while the program is being executed, while compiled languages have already been translated before program execution.


P.S.: I mentioned Java vs. C as interpreted vs. compiled languages. In fact, one cannot say, "This language is compiled, and this one is interpreted." While such statements are generally true, there's nothing to stop someone from writing a C language interpreter. In fact, an interpreter for C exists, though I can't presently remember it's name.

stakx
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
@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.
GrayWizardx
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?
Personman
@Personman: AFAIK, Java byte-code is interpreted by the JVM, as @GrayWizardX just said. See also my P.S. -- And yes, the distinction can be somewhat arbitrary today. Contrast the Java VM with e.g. the .NET "VM". .NET programs are first compiled into CIL byte-code (at "compile-time") and again compiled to machine code (at "run-time"). ... Does this now make C# a compiled language, or an interpreted one?
stakx
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.
Bill the Lizard
I guess I think that in general, since at some level any running code is interpreted in some form by something, that it is most useful to mean by 'interpreted language' 'language for which the standard use case is to run an interpreter rather than a compiler on the source itself.'
Personman
@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.
Stephen C
@GrayWizardx: s/OS/CPU/. The OS is responsible for resource management, not code interpretation; it does not itself execute code, it is executable code.
outis
See also: http://stackoverflow.com/questions/1326071/is-java-a-compiled-or-an-interpreted-programming-language
outis
@Stephen C: Thanks for the clarification, I wasn't aware of that. Shows how out-of-date my Java knowledge is...
stakx
A: 

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, compled 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"

Salil
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.
GrayWizardx
A: 

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.

Personman
+6  A: 

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

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.

Norman Ramsey