• Post Reply Bookmark Topic Watch Topic
  • New Topic

java is compiled or interpreted?  RSS feed

 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Earlier I used to think that java is compiled as well as interpreted but I got confused when I got across an article in wikepedia which states
The JIT compiler reads the bytecodes in many sections (or in full, rarely) and compiles them dynamically into machine language so the program can run faster. Java performs runtime checks on various sections of the code and this is the reason the entire code is not compiled at once.[2] This can be done per-file, per-function or even on any arbitrary code fragment; the code can be compiled when it is about to be executed (hence the name "just-in-time"), and then cached and reused later without needing to be recompiled.Just-in-time_compilation
.(see overview section)

I know this has been discussed a lot of times but still I request the forum members to help me clarify my concepts.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A language doesn't have an inherent property of being compiled or interpreted. Any language could be executed either way. (Or at least most languages. There may be some subtle detail that prevents it being universal).

In normal usage, Java source code is definitely compiled to Java bytecode--the machine code for the JVM. But then at runtime, that compiled bytecode is interpreted, at least at first, by the JVM. Some of the bytecode will be compiled to native code on the fly.

In the end, trying to classify Java as a "compiled langauge" or "interpreted language" is an oversimplification, and pointless.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, It is said that java is interpreted.
as you have often used the command javac that stands for the compiler.
and the command java that is the interpreter, as you know the interpreter reads the byte code line by line and produces the output.
The work of compiler is to check the syntactical error in the program obviously according to java programming rules.

For example in the code below :- your output will be Hi but when you change the value of j =0; than it will terminate from that very line (8) of code it means java interpreter checks the code line by line and executes so java is interpreted.



Regards
D
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdinesh Tiwari wrote: and the command java that is the interpreter, as you know the interpreter reads the byte code line by line and produces the output.


Except, of course, as I already pointed out, and as the OP discovered for himself, modern JVMs also include a compiler. So by any reasonable definition, Java is both an interpreted and a compiled language, and in fact a lot of Java code gets compiled twice before execution.

when you change the value of j =0; than it will terminate from that very line (8) of code it means java interpreter checks the code line by line and executes so java is interpreted.


No, that doesn't show that Java is interpreted. You can get the exact same behavior from compiled code.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, Java is advertised as interpreted. It is one of the ten “buzzwords” which appear in that link, and which somebody got delightfully wrong here.

Things have changed a lot since that white paper was written, and the current version of the Java Tutorials doesn't say “interpreted”.
 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:A language doesn't have an inherent property of being compiled or interpreted. Any language could be executed either way. (Or at least most languages. There may be some subtle detail that prevents it being universal).

In normal usage, Java source code is definitely compiled to Java bytecode--the machine code for the JVM. But then at runtime, that compiled bytecode is interpreted, at least at first, by the JVM. Some of the bytecode will be compiled to native code on the fly.

In the end, trying to classify Java as a "compiled langauge" or "interpreted language" is an oversimplification, and pointless.


Hi Jeff,

Anyways a language has to stand in either category or both.

To me role of JIT is not clear.Please help me to understand its concept during java program interpretation.On one hand we are interpreting our java byte code via JVM interpreter on the other hand we are saying that it is JIT Compiled that is an actual compiler.

And as you said
1)java is first compiled to bytecode.
2)then at runtime byte code is interpreted or some byte code can be compiled to native code on the fly.

Does it mean that JVM needs to resolve whether it need to interpret or compile it via JIT during runtime??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam Bhalla wrote:
To me role of JIT is not clear.Please help me to understand its concept during java program interpretation.On one hand we are interpreting our java byte code via JVM interpreter on the other hand we are saying that it is JIT Compiled that is an actual compiler.

And as you said
1)java is first compiled to bytecode.
2)then at runtime byte code is interpreted or some byte code can be compiled to native code on the fly.

Does it mean that JVM needs to resolve whether it need to interpret or compile it via JIT during runtime??



Whether the bytecode is interpreted or compiled is an implementation details... basically...

With the -client JIT, the bytecode is immediately compiled to native code.

With the -server JIT, the bytecode is interpreted (while collecting profiling data). After a while, the bytecode is compiled (in an optimized fashion) to native code.

There is also a new -tiered JIT, which uses the -client compiler to generate temporary native code (that also profiles) and then the -server compiler is used to do an optimized compilation.

Of course, you can disable all this JIT stuff -- and configure the JVM to interpret only...

Henry
 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hye Henry,

From your post,It got the following flow->

compilation --------->then interpretation of byte code------>compilation of temporary native code + system profiling information---->Program execution.

Anyways If these are the steps then I got the concept clearly.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam Bhalla wrote:
Hye Henry,

From your post,It got the following flow->

compilation --------->then interpretation of byte code------>compilation of temporary native code + system profiling information---->Program execution.

Anyways If these are the steps then I got the concept clearly.



My last post provided four different JVM configuration options -- all with different execution flows. Not sure how you boiled it down to one.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam Bhalla wrote:[
Anyways a language has to stand in either category or both.


No, it doesn't. As I already stated: 1) Compiled vs. interpreted is NOT a property of the language, but of the runtime. It's association with the language is merely by most common use and currently available tools, and 2) There are different meanings that can be applied to "language X is compiled/interpreted" and which meaning you apply will determine how you answer the question. So, once again, there is no single right answer to "Is Java compiled or interpreted?"


And as you said
1)java is first compiled to bytecode.
2)then at runtime byte code is interpreted or some byte code can be compiled to native code on the fly.

Does it mean that JVM needs to resolve whether it need to interpret or compile it via JIT during runtime??


The JVM doesn't have to do anything as far as compiling bytecode to native vs. interpreting bytecode. A given JVM implementation could be all compiled, all interpreted, or a mixture of both. Most modern JVMs are a mixture of both, but I think the very first ones only interpreted the bytecode. How a particular JVM implementation decides whether or not to compile some or all of the bytecode it's executing it entirely up to the designers of that implementation. A JIT is one particular approach to deciding what to compile and when.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautam Bhalla wrote:I know this has been discussed a lot of times but still I request the forum members to help me clarify my concepts.

As you've probably gathered, the lines of "compilation" and "interpretation" have become blurred, especially with the advent of the JVM and new generations of "dynamic" languages.

Is an interpreter that compiles some of its code actually an interpreter, or just really smart? Indeed, is any interpreter that works from bytecode actually "interpreting", or simply translating (or in many cases transliterating, which is slightly different again).

The fact of the matter is that you have to get from source code to machine code somehow. In the olden days that meant either compiling and linking or, in the case of scripting languages, running a program that really would "interpret" your source on the fly (or JIT, as its now termed) and hand off directly to system routines.
JVMs have blurred all that, because they have an enormous amount of smarts built into them, which not only allow them to "interpret" bytcode, but actually improve it, through many of the techniques already mentioned.

However, getting back to your question, my answer is: Java is a compiled language, because there are two stages to the process.

Nothing's gonna happen unless you run javac first; and that's where you sort out syntax issues - just like in the old days.

Once I have my .class file I hand it off to java and the JVM, and I really couldn't care less if they do another 17 levels of compilation or "interpretation" or "being really clever" - as long as it runs.

Winston
 
Gautam Bhalla
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all, I have already got what I expected from this forum..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!