Java is not a language with training wheels. It is a language for professional programmers.

— Hebert Sheildt

How Is A Java File Executed

Java is a programming language which has a different way of executing the code. Java is a compiled as well as an interpreted language unlike other programming languages like C, C++, etc.

It might interest you to know that any empty Java file is a valid Java file and it does compile successfully.



Empty file “Test.java”.



“Test.java” compiles without any errors.

However this file wont execute as it does not have a main() method. Just because a file compiles, it does not mean it will execute successfully.

When a Java file is created, it is compiled using the “javac” tool. This tool creates a “.class” file. I would like to clarify what platform independence is in context to Java. Here I would like to point out that this “.class” file is platform independent. For instance you generate this “.class” file on Windows OS, you can take this “.class” file onto any OS like Linux or Mac, it’s the responsibility of the JVM (Java Virtual Machine) that is installed on Linux or Mac OS to execute this “.class” file.

Again, JDK (Java Development Kit) is not platform independent, for every OS a respective JDK is available for download. A JDK for Mac OS won’t run on Windows platform. But any Java application can be executed on any platform.

Hence it’s said “Write Once, Run Anywhere” (WORA).

After using the “javac” tool for compiling the Java program, we use the “java” tool to execute the generated “.class” file. The “.class” contains byte code. Here it gets interesting; when the “java” tool is used it interprets the byte code i.e. the “.class” file. Interpretation goes line-by-line, whereas compilation happens all at once. Interpretation is a relatively slow process as compared to compilation. In the earlier versions of JDK, the time taken to execute Java programs was relatively high, which is a performance hit. Sun later realized this issue and brought in the concept of JIT (Just-In-Time) compiler.

This JIT compiles certain part of the byte code; not all the code is compiled, only certain code is complied which would benefit from compilation. Eventually certain part is compiled and the rest is interpreted.

© Copyright © 2013 SourabhBawage.com. All Rights Reserved.