What differs between "writing a specific JRE for each platform" for Java developers and "writing a C++ compiler for each platform" for C++ ones?
migrated from stackoverflow.com Sep 27 '11 at 15:42
|
Java is compile once run anywhere. C++ is write once compile anywhere. |
|||||||||||||||||||||
|
|
"writing a specific JRE for each platform" is not something you do everytime. Porting the JRE to a new platform is something you need doing only once. This task is generally done by the core maintainer/developers of the program and/or the platform. A lot of factors may come into play when deciding who and how the JRE would be ported. Among other things, it depends on the licensed it's published under (I hear Java is Open Source, so I guess anyone could do it). Funny anecdote, Steve Jobs made a big deal about not wanting to take care of the porting of Java on Mac, around a year ago. The point is not how or who ports the JRE, but the fact that once it is ported, every Java application should now theoretically run easily on the new machine. In that sense, the JRE forms an abstraction layer, completely hiding the machine, allowing easy porting. However, reality is not always pretty like this. I won't go as far as calling portability a "myth", but it's true that it's not so perfect. For instance, Java has a package called As mentioned in the comments, C++'s approach to portability is different. On the one hand, it's a compiled language, and those binaries are almost always platform specific. So c++ executables will never be portable (unlike Java). On the other hand, porting the compiler can sometimes be enough. The community has found that by porting the compiler as well as some core libraries of the language, source codes (and not binaries) could be portable. However, C++ is widely used in critical systems like compilers, kernels, real-time systems,embedded systems, ... There's a "low level" aspect of C++ that cannot be overlooked, when talking about portability. |
|||||||||||||
|
|
It's not just the language -- it's the libraries. Both Java and C++ provide cross-platform libraries. Java provides a richer set. |
|||||||||||||||||||
|
|
The difference is that Java will run on any platform without recompiling. Having a C++ compiler for each platform isn't the same at all. |
|||
|
|
|
All the answers starting with "The difference is...", or anything very similar, are basically wrong (sorry, but such is life). There are really two separate differences between the two. One (that's been mentioned a lot) is that a compiled Java program can (or least should) run on any conforming implementation of Java, so even after being compiled, you can still move a Java program from one platform to another without re-compiling. C++ (at least normally) requires re-compilation for each target platform. The other is that Java (at least attempts to) assure that all correctly written Java will be portable. At least in theory, you shouldn't be able to write any code that isn't portable. C++ allows you to do quite a few things that aren't portable. The C++ standard contains "warnings" about a lot of things that aren't portable (e.g., telling you that you'll get implementation defined behavior or undefined behavior), but it doesn't necessarily try to stop you from doing them at all. Just for example, if you want to write an operating system for hardware that uses a PCI bus, you're probably going to need to read/write the PCI configuration memory. This obviously won't be portable to systems without a PCI bus, but if you're writing an operating system for hardware with a PCI bus, it's pretty much necessary. C++ allows it even though it obviously won't be portable. |
|||||||||||
|
|
You have misunderstood the premises. Java programs are very portable, because the JVM provides a standard behaviour guaranteed to be the same. C++ programs have a less standardized environment closer to the actual hardware, so the program needs to be able to handle the various platform specific details - like size of an int, word alignment etc etc etc. The JVM itself is not very portable. It is a herculean task to port a high-performant JVM to another platform or CPU architecture. |
|||
|
|
|
The difference is that Java (it's not an acronym) programs can be distributed in a form that can be run on any computer with a JVM installed, but C++ is normally distributed as either source code, which is very user unfriendly, or as a bunch of different binaries for different platforms. |
|||||
|
|
The amount of work for the supporting tools is indeed similar, the difference lies elsewhere. Once a C++ program has been compiled for a platform, you have to compile it again if you want to use it on a different platform. However, when a java program has been compiled, you can move it to any other platform with a runtime environment without having to recompile it. |
|||
|
|
|
Answering the title "Is portability a myth ?", instead of "Which is better at portability, Java or C++", I'll say that partial portability is possible, but full portability it's does a myth. Something that I insist to write, and it applies to this question, is that developers are not longer working with just programming languages, but, with full programming frameworks. And those frameworks, include libraries, databases, graphical interfaces. Which programming language, or which programming framework is more portable ? Well, it depends, on what your app. is trying to achieve. |
|||
|
|
|
The thing is "you" don't write the JRE you write the Java code which runs on any JRE. "You" do write the C++ code which can require changes by you before it will compile on another platform. |
|||
|
|
