I’ve been coding with Java for a few years now and newer students often tend to ask me “Is Java a compiled programming language?”
The answer is not what you might expect, so let me clear up any misconceptions you may have.
Is Java A Compiled Language?
Java is both a compiled language as well as an interpreted language. The Java code is written in files with the extension .java. This source file is compiled by javac, i.e. the Java compiler into a class file. However, unlike C or C++, the Java compiler generates bytecode instead of native machine code.
Introduction to Java
JAVA is a programming language developed by Sun Microsystems back in 1991.
The main objective of JAVA is to write code once and then have the ability to run it on many operating systems. Since 2006, JAVA has been available under GNU General Public License by the name of OpenJDK.
Java comprises a defined specification and consists of a programming language, compiler, core libraries, and a runtime Java Virtual machine.
The runtime allows the developers to write code in languages other than Java programming language which runs on the Java virtual machine.
Features of Java
A few salient features of the Java programming language are as follows:
- Platform Independence: Java is a highly portable language because the Java programs use the virtual machine only as an abstraction without directly accessing the operating system.
- Support for Object-Oriented Programming: Java is considered as one of the best object-oriented programming languages out there. Other than the primitive data types, all elements in Java are treated as objects.
- Strongly typed: All variables in Java must be predefined, and the coder mostly does the conversion to any other object type.
- Automated Management of Memory: Java is known for excellent memory management. It automatically allocates and deallocates the memory for new objects. The garbage collector automatically deletes any object with no active pointer.
To better understand whether Java is an interpreted language or a compiled language, let us first see what these terms mean.
Compiled languages are the ones that are directly converted into machine code that the processor can readily execute. This direct translation makes them extremely fast and efficient compared to the interpreted languages.
Hardware control is much better with such languages as they give the programmer complete control over aspects such as CPU usage and memory management etc.
One overhead with compiled languages is that they need to be “built,” i.e. manually compiled. For every change you make, you have to “rebuild” the program.
An interpreter runs through the entire program line by line and executes each command. Each line of the code is read first, and then the appropriate action is taken.
As it is quite apparent, interpreted languages were extremely slow compared to compiled languages back in the day. However, a new technology known as just-in-time compilation has reduced the speed gap between the two.
Verdict on Java: Compiled or Interpreted?
Many programming languages can have both implementations, i.e. compiled and interpreted. The language itself is not necessarily compiled or interpreted but is referred to as such for simplicity. Similar is the case with Java.
It becomes difficult with Java to assign an explicit category like other languages such as C or C++. Java neither generates machine language code after compilation of the source file nor does it execute line by line.
Java is both a compiled language as well as an interpreted language. The Java code is written in files with the extension .java. This source file is compiled by javac, i.e. the Java compiler into a class file.
However, unlike C or C++, this Java compiler does not generate native machine code. Instead, it generates Bytecode which is different from it.
The main difference between machine code and Bytecode is that machine code is a binary code that the CPU can directly execute. On the other hand, Bytecode is a non-runnable code that comes out of a compiling source and needs an interpreter to run.
Similarly, the Bytecode compiled by the javac is interpreted by the Java Virtual Machine or JVM during program execution.
Our discussion concludes that Java can not be explicitly categorized into compiled or interpreted like other languages. It is considered both a compiled as well as an interpreted language.
If you want to learn Java, check out these 10 awesome Java projects. There is a full video tutorial that walks you through each project, step-by-step. And it’s absolutely FREE!