Reputation: 105
C++ is not platform-independent because it needs to be compiled into native code and a different compiler needs to be implemented for different CPU architectures.
A C++ compiler only translates the code into machine language or native code. This native code is executed by the processor.
From what I read on the net, a Java program is initially compiled into Bytecode by the java compiler 'javac'. This bytecode is then interpreted (and executed) on a line by line basis by the Java virtual machine.
I have few queries.
1) If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
2) If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
3) I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
Upvotes: 1
Views: 488
Reputation: 2423
In a sense you're right, in that you can essentially equate a C++ compiler to the combination of a Java compiler plus Java virtual machine. Neither of these two are in themselves platform independent: a Java compiler written for a Linux machine will typically not run on a Windows machine, and neither will a JVM.
However, in practice the intermediate language of Java bytecode, which is what the Java compiler produces and the virtual machine executes, makes a huge difference because it is on this level that compiled code is shipped. So if you get a Java binary, it is bytecode. Provided you have a JVM for your machine, you can run it without further ado. In contrast, a C++ binary is machine code for a particular machine; you cannot run it on any other machine than that which it was compiled for.
Now, to come to your actual questions:
Upvotes: 2
Reputation: 533520
C++ is write once, compile anywhere (at least in theory, easier said than done, but it is done)
Java is compile once, run any where (provided a JVM is installed, again there are differences between systems which cannot be hidden entirely)
If C++ is not platform independent because different compilers need to be designed for different CPU architectures, then doesn't JVM differ from one hardware to another as well?
The JVM differs but the byte code doesn't have to. The C++ can be written once but often has platform specific code. This doesn't have to be a problem, but you can expect that the code has to be tested, an custom code written for each flavour of system it is written for.
If I wrote a C++ code and compiled and executed it on Machine 1 and Machine 2, the output produced will be the same. Similarly, if I write a Java program and execute it on two different machines, the output will still be the same. Why the extra step of bytecode generation?
You only need to compile it once for Java. You can take a JAR compiled using Java 1.0 on a 32-bit windows platform running Windows 95 twenty years ago and use it immediately without the source or knowing how it way compiled, on a 64-bit ARM processor running Linux and it will use the latest optimisations and instruction set for that processor.
For C++, if you take a program written on Windows 95 twenty years ago, it probably won't compile without a lot of work and testing on a 64-bit ARM processor for Linux.
I read somewhere that interpreters, unlike compilers, actually execute a program by emulating a virtual machine. Does it mean that JVM actually executes the bytecode and not just interprets it into native code?
The JVM interpretes initially, compiles it quickly, then slower (Called C1 in the OpenJDK) and later re-otimises more aggressively (Called C2) It goes through 4 different levels of compilation and it can de-optimise code to have it re-optimised based on changing usage and even changing code.
Upvotes: 2