aspose file tools*
The moose likes Beginning Java and the fly likes jvm is an interpreter..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jvm is an interpreter....." Watch "jvm is an interpreter....." New topic
Author

jvm is an interpreter.....

krishna Gajarla
Greenhorn

Joined: Oct 02, 2005
Posts: 21
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.


Thanks and Regards,<br />krishna<br />------------------<br />Charles Darwin: <br />"It is not the strongest of the species that survives, <br />nor the most intelligent, <br />but the one most responsive to change"
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
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.


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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-


You down with OOP? Yeah you know me!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Aug 19, 2005
Posts: 2906
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

Joined: Jul 11, 2001
Posts: 14112
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...)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Jul 11, 2001
Posts: 14112
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jvm is an interpreter.....
 
Similar Threads
What is JVM?
in JVM what is the need of Just in time compiler and Java Interpreter?
Jdk and Jvm
javac and java (compiler and executable)
JIT compiler