• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jvm is an interpreter.....

 
krishna Gajarla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi every body,

can you please clarify between an interpreter and compiler.


jvm is an interpreter.
is it correct?



what does a compiler do ?
in the case of c language we call as c-compiler.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by krishna Gajarla:
jvm is an interpreter.
is it correct?


It could be but in most cases it isn�t today.
An interpreter reads program code translates it to the CPU�s native instruction set and then executes. Even if it read the same section of code again and again it will translate that same section of code � not reusing previous translations.
A compiler translates all the program code to the CPU�s native instructions (or even a different representation) at once before execution. So when you execute you are executing the CPU�s instructions � no translation required.

javac is a compiler that translates Java source code to Bytecode. Bytecode cannot execute on most processors (unless the processor is a solid state JVM) so it is passed to the JVM to run. Most JVMs today utilize JIT (Just In-Time compilation) technology so the bytecode source is only translated once to the native CPU instruction set during the same execution run � after that the generated CPU native instructions are re-used. The generated code is discarded once the code terminates it's run.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without getting too bogged down in the details, both compilers and interpreters do a very similar thing.

all programs must be in the native language of a machine to run. that's the only language a machine understands. Not Java, not C, not even Assembly.

when you write a program in C, you write it using the rules defined for the C language. it's something a human can understand, but not a machine.

the C compiler is a program that translates the C text file into machine language. you must have a C compiler for the specific OS/hardware you want to run your program on. once the code is compiled, it can be executed by the machine at a later time, but the important thing here is that the compiler generates a file that can be executed.

An interpreter is a program that runs at the same time as your program. basiccally, it converts a file into the machine language needed to run the program.

Java kind of uses both. your .java files are run through the java compiler (javac) to create your .class files. this is a sort of intermediate language that isn't really readable by humans, nor can any OS/Hardware run it directly.

when you call "java HelloWorld", the java interpreter reads the .class file and converts it to machine language as needed.

the reason java works this way is that you only need (theoretically) to compile your source once. that .class file is in a language the JVM understands. as long as htere is a JVM, your same .class file can be run on any machine... even machines that don't exist today.

think of it like an international newspaper. articles are written in english. the javac compiler converts the English into Esperanto. that esperanto version is sent all over the world, where Sun has put poeple who can translate Esperanto into the local language.

I don't need to send the French version to France, the German Version to Germany, and the Chinese verison to China (oh, and WHICH dialect of Chinese do i send???)). i send the SAME Esperanto/.class file to EVERYONE, and let them trnaslate it when they need to.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interpreter is a program that runs at the same time as your program. basiccally, it converts a file into the machine language needed to run the program.


That's not my vision of an interpreter at all. It does not convert a file to machine language. Instead an interpreter looks at a language instruction and does the action that code represents.

The JAVA compiler converts something like "a = 3 + 5" into several byte code instructions. An interpreter reads byte code and perform the appropriate action for each instruction.

This is often really slow. Modern JVMs might interpret something once or twice and then the next time through say, "If you're going to do this a lot I'll compile it." Then they generate a bit of real machine language and execute it the next time they hit that line of code.

If we say "compiling" means generating machine language, this is a bit confusing. Should we call the Java compiler a compiler if it doesn't put out machine language? We can justify the name by saying it puts out machine language for an imaginary or virtual machine. Maybe the interpreter part of the JVM should be called an emulator instead.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll add my own question to this post, I thought that the javac compiler..
was an interpreter within itself.

I believe thats what my prof said in CS I.

because the compiler is the interpreter, in the sense that it takes your
code and checks it through certain regulations, then tells you if your
syntax is correct or not.

even though the compiler can interpret your syntax, it can't interpret your
semantics...

please clarify this for me...

-Justin-
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interpreter reads byte code and perform the appropriate action for each instruction.

I'm willing to admit i'm not the expert here...

but, in order for the interpreter to "perform the appropriate action", doesn't the machine language code need to be sent to the CPU? maybe there's not a literal interpretation, but somehow that basic instruction that the CPU understands has to be sent to the CPU.

or am i totally wrong?
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
I thought that the javac compiler was an interpreter within itself.

I believe thats what my prof said in CS I.

because the compiler is the interpreter, in the sense that it takes your
code and checks it through certain regulations, then tells you if your
syntax is correct or not.


In the context of the english language at large you could make a case like that. However in the narrowed context of code generation/execution that is simply not the accepted usage of the word. Javac is a compiler - just because it performs syntax and security checks doesn't make and "interpreter". Maybe your prof was also teaching some english courses at the time.

Interpreter
Compiler
JIT Compiler
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
I'm willing to admit i'm not the expert here...

but, in order for the interpreter to "perform the appropriate action", doesn't the machine language code need to be sent to the CPU? maybe there's not a literal interpretation, but somehow that basic instruction that the CPU understands has to be sent to the CPU.


In contrast to a compiler, an interpreter typically won't create any new byte code that gets send to the CPU, it is its own byte code which gets executed to perform the appropriate action. (I tried to translate this to the newspaper analogy, but it really breaks down here, because both the compiler and the interpreter are too executed by the CPU...)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's like the difference between reading an English recipe and writing it out in Russian vs reading English and baking a cake.

Unfortunately my metaphor for compiling is commonly called "interpreting" and my metaphor for interpreting is just baking. Gotta keep thinking here.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Maybe it's like the difference between reading an English recipe and writing it out in Russian vs reading English and baking a cake.


I think I have it:

Compiling is like first following a text that says "if you encounter the words 'Backofen �ffnen', translate it to 'open the oven'", and *then* following the resulting text to bake the cake.

Interpreting is like following a text that says "if you encounter the words 'Backofen �ffnen', open the the oven".

How does that sound?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic