Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Wednesday, May 10, 2023

Is Java Compiled or Interpreted Programming language? Answer

One of the first question a graduate C or C++ programmer, who has just started learning Java ask is, whether Java is a compiled language or an interpreted one? On academic courses or during college, students learn a lot of languages like VB, C, C++ and they happily categories them as either compiled or interpreted, but with Java it's tricky. It's not clear whether Java is compiled or interpreted, because it neither generates machine language code after compiling a source file,  neither interpreted source file to execute instruction line by line. 

In order to answer this question, you need to first know that Java is a platform-independent language? This means you can run a Java program to any platform, which includes hardware + operating system, without any modification. Knowing how Java achieves platform independence is key to answer this question.

If anyone asks this question during interview, then your answer should be both i.e. Java is both compiled and interpreted programming language. Java code is written in .java files (also known as the source file), which is compiled by javac, a Java compiler into class files.

Unlike C or C++ compiler, Java compiler doesn't generate native code. These class files contain byte-code, which is different than machine or native code. Java virtual machine or JVM interprets byte codes during the execution of the Java program.

So, you can see it's both compiled and interpreted language, but this answer is incomplete until you mention JIT (Just in time compiler) which does another round of compilation to produce native code, which can directly be executed by the corresponding platform.

We will learn about how JIT works in the next section.

Java is both Compiled and Interpreted Language but how?

The above answer is absolutely correct but it's not complete. What javac (Java compiler, which comes along JDK) does is a pseudo compilation, it doesn't convert Java source code into native code, which can directly be executed by CPU, real compilation into native code is done by another program called Just in Time compiler, also known as JIT. This is actually an optimization done on JVM by Java platform engineers.

When JVM interprets Java byte code, it also gathers useful statistics, like which part of code is hot and always run. Once JVM has enough data to make such a decision, JIT can compile that part of code e.g. method or block into native code.

This native code will then directly be executed by a machine, without interpreting by JVM. JIT provides an immense performance boost to Java applications and this is one more reason why Java is also used to write high-performance applications like electronic trading systems, algorithmic gateways etc, along with native languages like C and C++.

In short, Java is both compiled and interpreted language. It compiles using javac and JIT and interprets using Java virtual machine. Here is the sequence of things normally happens from writing to execution of a Java program :

1) Programmer write source code and store that into a .java file. Always remember the name of your Java source file must be the same as the public class declared inside that file, for example, if there is a public class called Order inside the Java file, then it name must be

2) javac compiles .java fie and creates class files, which contain byte code. This byte code is main reason of Java being platform-independent.

3) JVM executes these class files and gathers statistics of execution run. These statics are used to determine hot spot i.e. part of your code which executes 90% of the time.

4) After a certain threshold, when JVM has enough data to make a decision, JIT compiles frequently used byte codes into native code, which is then directly executed by the platform. This provides a performance boost to Java applications.

Is Java Compiled or Interpreted Programming language? Answer

That's all folks, You can now say that Java is both compiled and interpreted language. Some people even called Java a dynamically compiled language, referring to compiling byte code into native code during runtime. While answering this question during the interview, always remember to mention about JIT compilation as converting Java source code to class files is not really compilation in true C and C++ programming sense, where compiler produce native code.

Other JVM Internal Articles you may like
  • 10 Essential JVM options for Production apps (article)
  • Difference between Stack and Heap memory in Java? (answer)
  • Top 5 Courses to learn JVM internals in-depth (courses)
  • How Garbage Collection works in Java? (article)
  • Top 5 Books to learn about Java Memory Management and GC? (books)
  • Difference between 32-bit and 64-bit JVM? (answer)
  • What does -XX:UseCompressedOOPS does in 64-bit JVM? (article)
  • 10 points about heap memory in Java? (article)
  • When a class is loaded or initialized in JVM? (article)
  • How do you find CPU and Memory usage of Java applications? (article)
  • How Classloader works in Java? (article)
  • Top 5 Advanced Java Courses to learn Performance? (courses)

Thanks for reading this article so far. If you find this Java article useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 


Unknown said...

Hi Javin,
I needed a clarification regarding the JIT compiler. The JIT compiler converts the frequently used part of the code's bytecode into native code which does not need to be interpreted by JVM? Is this part cached or somwthing for future executions?What about the remaining part of the code? That is simply converted by the JVM from byte-code to machine level instructions ?
I am little unclear about what you mean by native code v/s machine level instructions to signify the importance of JIT compiler. I suppose they are the same.Could you please elaborate the same.

Unknown said...

Also i suppose this JIT compiler is part of JRE. How can this JIT compile the source file to macine level instructions directly? It is only provided with .class files.

YourFriend said...

Thank you so much for this information and great explanation

I never thought about that question whether Java is interpreted or a compiled language.

Anonymous said...

This article is really examining the wrong question. The only reasonable question is "Is Java an interpreted language?" This is asking whether Java programs are compiled to machine code or to an intermediate form that must be interpreted by a program (called an interpreter) running on the machine.

Virtually all programming languages are compiled. Directly interpreting programming language source code would be so inefficient as to be useless. The only valid question is what the language is compiled to.

Anonymous said...

IMO there are two ways in which JVM optimizes the bytecode ,one is using JIT compiler in which it compiles every method it counters for the first time and then uses that compiled code for execution but this takes up a lot of memory , another technique it uses is called 'adaptive optimization' in which it identifies the code which is executed frequently and it optimizes that bytecode. This saves a lot of time.

Unknown said...

I completely agree with Paul Topping and I have one query based on following statements:

>>> JVM interprets byte codes during execution of Java program.
>>> JIT (Just in time compiler) which does another round of compilation to produce native code.

Are you suggesting that during execution there is part of byte-code that gets interpreted by JVM while for some part of byte-code JVM decides to use JIT to convert it to native code and run it directly on machine?

If that's true then that means that for a given java application a part of code might be getting interpreted
while part of it might be directly getting executed on machine. Is my understanding correct here?

Amitesh Rai said...

@Sachin Tiwari, The above article is correct.
When the JVM executes a byte code, then it uses a program called "Profiler". The role of this profiler is to identify the block of codes that are being executed frequently. To identify such block of codes, the Profiler uses a counter variable. When the value of this counter has reached a threshold value for a piece of code that is being executed frequently, Then JIT compiler comes in action and executes that piece of code into native machine code and cache it for future use.

Unknown said...

Java AOT

Post a Comment