Saturday, 14 June 2014

Is Java Compiled or Interpreted Programming language?

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 e.g. 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 generate machine language code after compiling source file,  neither interpreted source file to execute instruction line by line. In order to answer this question you need to fist know that Java is a platform independent language? Which 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 ask 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 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 contains byte-code, which is different than machine or native code. Java virtual machine or JVM interprets byte codes during execution of Java program. So, you can see it's both compiled and interpreted language, but this answer is incomplete until you mention about JIT (Just in time compiler) which does another round of compilation to produce native code, which can directly be executed by corresponding platform. We will learn about how JIT works in next section.

No comments:

Post a Comment