Platform Dependent and Independent Language.

C++ Vs Java compilation

First question, What is a platform?

It is a combination of the processor and operating systems basically, which can be used to host any application or service. The most common example of operating systems are Windows, Linux, and MacOS.

Second question, What is a language?

Our machines understand nothing but 0's and 1's, the binary form. Since we cannot write a set of instructions to our machines in this binary form (we can though, but it will be very inefficient). We tend to write them in a way that makes more sense to us and then turn them into 0s and 1s for the computer to execute using something called a compiler.

Today we write our code in High-level languages (English-like commands which are more human-readable) such as C, C++, Java, Python, Powershell, and a lot more, we use these languages to give instructions to our computers so that they can accomplish certain tasks, these instructions put together in a sequence are called a program or a code. The code is then turned into machine language by a compiler or an interpreter which is nothing but 0s and 1s, this process is called compilation. Post that this set of instructions is carried out on the machine to carry out the task as instructed.

What do you think? can the same code you compiled on a Windows OS run on Mac? or Linux? or vice-versa? That is what we are going to understand in this article.

Note: You may be thinking about what is a compiler? and what is an interpreter? If you have not heard of it before. So without going into much depth about them, In one line if we have to explain this, then it would go like...

"A compiler turns the whole code into machine language at once, whereas an interpreter turns the code into machine language one line at a time, line by line."

Comparing languages dependency on platforms:

Let us take the two most commonly used programs for our comparison.

C++ :

The code written in C++ is directly converted into machine language by the compilers and is then executed by the CPU. The code runs efficiently (given that it's written well) and there is no problem with that, plus since the compilation of the whole code is happening at once, the execution time is significantly faster as compared to other High-level languages.

Where is the problem then? The problem arises when you wish to execute the same code over a different platform. You may be thinking, what is the issue with this? Just transfer the code to another machine and it will work, right? Yes, you are correct, however, this approach is prone to security breaches. let's see how.

If you share the uncompiled High-level written code which is also human-readable over a network, then there are chances of someone breaking into the file transfer network and getting the source file in between, and if you try to transfer via a human medium in a drive or any other storage device, there are high possibilities of it being compromised.

Now, If you are thinking, then simply just share the compiled code which is written in machine language, this is not in human-readable form as well, then there is no chance of security breach, as even if somebody gets their hands on it, they won't be able to read it, right? Yes, But that code will not be useful to them.

How? That is because the code has been compiled on a different operating system as per its kernel. Each operating system has different functionalities specifically designed to serve its compatibilities, the C++ compiler simply doesn't have the functionality to compile the code in such a way that makes the code useful to any other platform post compilation or create any intermediate file.

Then, what is the solution? That we will try to see it in our next example.

JAVA Compilation:

The code written in Java is not directly converted into machine language. It is first compiled into an intermediate file, which we call 'bytecode', this bytecode is neither human readable nor it is in machine language.

Since that bytecode is not in human-readable form, it is more secure for transferring it over to another platform, via a network share or any other medium, as through bytecode any person won't be able to retrieve the source code. This solves our issue of cross-platform functionality.

The bytecode is then, picked up by the JVM (Java virtual machine) for conversion into a machine language, and is interpreted line by line. Since the code first gets compiled first and then interpreted line by line, it is arguably slower than C++, but when you see that it needed to be compiled only once and not repeatedly, in the long run, the execution time won't matter it will depend on the use case.

Note: The bytecode itself is platform independent, However for it to run successfully you need to have the JVM installed on that particular platform, then only you will be able to achieve platform independency, as JVM is designed specifically for each platform, hence it is able to convert the given bytecode into required machine language for that particular platform. It is to be noted that the Java compiler is also known as a hybrid compiler as it both compiles and interprets at different levels.

The above reasoning only gave Java the term 'WORA' which stands for Write Once Run Anywhere.

\============================================================

This completes our discussion on platform dependency and Independency for now, we will dive further going forward.

I hope that, the above comparison made sense to you, I tried to keep it short and not use difficult terms, However, if you still have any queries/feedback, you can always connect with me over LinkedIn, click here to connect.

Now you tell me, can you run PowerShell on Linux? or Shell Script in Windows? what about Python? Is it platform-dependent or Independent? try to find out. Keep learning, and keep exploring!!