In my quest to understand the reasoning to use Java over C++ or vice versa for writing applications, I became stuck on this one point. I watched a video introducing me to Java and they said the reason there is a JVM is because it makes Java portable, meaning it runs on many architectures like phones and stuff without having to write a new compiler for each different CPU OS combo. But people must have to do something to make it compatible with each CPU and OS. I don't see how using a JVM changes anything.
-
I think the statement "compilers are expensive" requires some qualification to have any meaning. – Oliver Charlesworth Aug 10 '16 at 22:45
-
I think you're correct, I edited it. – Lv99Zubat Aug 10 '16 at 22:48
-
2It doesn't sound like the video is explaining it very well. – juanchopanza Aug 10 '16 at 22:49
-
The JVM essentially *is* a compiler, and modern JVMs are quite complex ones. Sounds like the video producer isn't familiar with the technology. – chrylis -cautiouslyoptimistic- Aug 10 '16 at 22:53
-
Yeah, the video wasn't thorough enough for me, nor could I find the answer anywhere else, which is why I'm here :P – Lv99Zubat Aug 10 '16 at 22:56
-
3A JVM largely consists of library code which can be made largely platform-independent. Ignoring HotSpot, JITs, etc., the task of porting to another platforms consists only of implementing the platform-specific parts for that platform. This is indeed much cheaper than implementing a compiler. I don't find the assertion that 'compilers are expensive' even slightly controversial in this context, unless you include script-kiddy tools which aren't relevant to the discussion. – user207421 Aug 10 '16 at 22:59
-
1@EJP - Though a fairer comparison would be "porting the backend of a compiler" - it's unclear why that would be substantially more expensive than porting the platform-specific components of a JVM. – Oliver Charlesworth Aug 10 '16 at 23:12
-
@OliverCharlesworth It would indeed be a fairer comparison, but porting library code requires lower (or at least more common) skills than retargeting compiler backends. It doesn't sound like the video has much of a clue about compiler architectures. – user207421 Aug 11 '16 at 00:24
-
Can anyone give me advice as to how to edit this question such that it is not opinion based? I fail to see how saying what is different about two things is a matter of opinion. I took out the part (see edit) that may have led someone to think the question is opinion based even though it wasn't substantial to the answer of the question. – Lv99Zubat Aug 12 '16 at 15:59
1 Answers
The initial (BETA) versions of Java were derided for being "write once, test everywhere". Improving the quality of the software on the initial platforms (SunOS/Solaris, Windows, Mac, etc) greatly increased its' portability (and portable software is inherently easier to port). Currently, there are multiple implementations of Java (IBM, JRockit, etc).
Fundamentally, Java is the specification of a virtual machine that executes byte codes; and not any particular implementation. Because the byte code is the same across platform the Java compiler doesn't need to be altered to support new platforms. Further, much of the OpenJDK (and, thus, the Oracle JVM) is written in Java. Basically, bootstrapping Java is simplified compared to bootstrapping a compiler for a "real" CPU architecture1.
1Excluding CPUs that natively execute Java byte code.

- 198,278
- 20
- 158
- 249
-
Correct me if I'm wrong, but the JVM is most likely written in either C or C++. I have to run the JVM on an architecure. So it doesn't make a difference whether I run Java/JVM on an architecture or straight C/C++. Either way I need a C/C++ compiler written for the architecture. – Lv99Zubat Aug 11 '16 at 15:15