Let’s talk today about the most interesting difference between Java and Python. Java vs Python: We have heard that Java is compiled language and Python is interpreted language. Although this is true to some sense but this does not means that Java does not have interpretation step or Python does not have compilation step. Both these languages do compile at a certain step and interprets also.
So, what’s the difference? The only difference is that Java has a separate step of compilation which converts the source code into bytecode before actual execution. And at the time of execution, this bytecode is converted into machine code by JVM, executed and results are shown. In Python also the code is compiled but it is not performed as a separate step but is hidden from the developer. And it appears that Python source code is directly executed which is not true. Python code is also compiled into bytecode and then executed; the only thing is both these tasks are done during run time/execution.
In short, compiler converts one language code into another language. And interpreter interprets or tries to understand a code written in some particular language and executes it.
In essence, no language in itself is either compiled or interpreted. It’s just the implementation of that particular language which is either compiled or interpreted. Java, Python or any other language is just a language to ask computer to do some activities. Lets see below Java vs Python in terms of code execution.
Java vs Python: Let’s see how Java code is executed or we can say how the implementation of Java behaves:
Consider you write a very simple java program to print “Hello World!” and you save the code as HelloWorld.java. The next step is to compile your program by using command javac. Once this command is executed, your .java code (source code) is compiled/ converted into .class file which is your bytecode file. The next activity is for JVM (Java Virtual Machine) which reads this bytecode, converts it into machine language code and then executes it. The result is pretty obvious: Hello World!
Let’s dive into it a bit more:
- Write above program and save the file as HelloWorld.java. This is your source code.
- Next step is to compile this above program. To compile, run below command:
- javac HelloWorld.java
- This command would invoke the java compiler and would result in the creation of a file HelloWorld.class. This is nothing but the bytecode of your source code. Now if your program has more than one class defined, then for each class there will be a bytecode file. Note that this bytecode is platform independent since it does not have machine level details specific to platform. Hence this bytecode can be sent to any other machine/Operating System and can be used there.
- In Java, compiler compiles the code all at once. Hence if there are any compilation errors, then all errors will reported at once.
- Next is the task of Java Virtual Machine (JVM) which you can say is the default interpreter of Java language. JVM itself performs below tasks:
- Class Loader: Class loader component of JVM loads the bytecode file of class having main method. Obviously all other class files will be called as in when they are used in main method.
- Bytecode Verifier: This component checks the bytecode to avoid any run time errors.
- JIT: Once the bytecode is verified, JIT (Just In Time) compiler comes into picture. This compiler again transforms the bytecode into machine language
- Then the machine code is executed and output is displayed. Execution of compiled machine language code is very fast.
- Note that this JVM is platform specific(for Windows, Mac etc)
Few important points to summarise:
- Java code is compiled into bytecode which is platform independent. Hence it can be sent to any platform where it can be executed
- Now execution needs JVM to convert bytecode into machine language which is platform dependent.
- When you install JDK (any version), it already have compiler and JVM hence no need to worry about execution.
Java vs Python: So let’s see how Python code is executed or we can say how the implementation of Python behaves:
The most commonly used implementation of Python is CPython. This python is implemented in C language hence the name CPython. Now as we saw earlier that in Python compilation does not happen as a separate step but is hidden. So when we execute Python source code .py file, at the time of execution itself, first the Python interpreter, Python Virtual Machine compiles the source code into bytecode as .pyc file and then executes the bytecode. Please remember that Python code does not move into machine language form. Since source code is directly converted into bytecode and executed, hence PVM is needed for execution.
Let’s dive into it a bit more:
- Create a python program to print “Hello World!” and save this code as HelloWorld.py file
- PVM: Execute the source code which will invoke PVM into action. PVM performs below actions:
- Code is first checked for errors, if any
- If there are no code errors, then the code is compiled into bytecode which is nothing but HelloWorld.pyc file.
- In python, this compilation happens line by line as against full code at once in Java. Hence in Python, as soon as compilation step encounters first error, it will be reported and compilation will fail.
- Once the code is compiled, PVM executes the bytecode, i.e., HelloWorld.pyc file and output is displayed. Since bytecode is directly executed, it is comparatively slow than Java.
Few important points to summarize:
- Python source code .py file is compiled during execution itself into bytecode .pyc file but not as a separate step.
- This bytecode is executed and output is printed. Bytecode is not converted into machine language.
At last we can just say that it’s the implementation of the language which is compiled or interpreted. And both these languages have their implementation which is compiled or interpreted. For example in Java, we can teach JVM to compile those pieces of code which needs to be converted into machine language. And parts of code which are basic or easy can directly be executed from byte code. Same is the case with Python. PyPy implementation of Python compiled the code just the way Java does. And then the code is executed. Mostly interpretation is a preferred way when we are using dynamically typed language like CPython.