Search code examples
javacpascalqbasic

What are the advantages of using Virtual Machine compilation (eg. JVM) over natively compiled languages?


I've heard that the advantage of java is that people can write code, compile it for the JVM, and run it anywhere. Each person just needs a JVM app for their platform.

Of course, it looks similar to the current situation, where everybody has a compiler specific for their platform. So the advantage isn't explained by that. But I think I see the explanation.. the issue must be that in the java situation, you can't or weren't meant to access the real machine directly in an OS specific way.

I suppose it'd mean that in other languages, the code itself has to be amended depending on what computer it is running on.

Can anybody provide short examples of this like a Hello World program that demonstrates this? No doubt it'd be in non-java e.g. C

Since it's not something that'd -normally- happen in a Hello World program or most i've seen since the books I used on java, they were unfortunately "how to program" style books, and all the stuff in them didn't demonstrate it(perhaps 'cos they couldn't or didn't want to use java to demonstrate it!). Though they trumpeted it as a big advantage. I'd like to see examples of it.


Solution

  • I've put together some of the answers..

    While I haven't tested them.. I see nice examples that make sense to me, from within the answers, of

    Bruno provided an example in C

    #include <win32.h> (An OS specific line and code would have to be rewritten for a different OS) anything that's limited to using calls in stdio.h and a few others (are portable)

    Gary, spoke of a case with int. That in C, "an int is 32-bit on a 32-bit box. 64-bits on a 64-bit box" "the portable way is to use int32_t" and a point about C and assembly language.. I have asked around and found that if you go over the limit, it cycles back to 0. So, that'd be a case of code having a different effect on a different system and compiling, but perhaps not working as intended, and it having to be rewritten.

    Thorbjørn provided a link to examples of assembly language on different CPUs . Win32 ASM for 32-bit CPUs and Win64 for 64-bit. It has a hello world example in each, and says that it's not easy to convert them, since "In Win32, all the parameters are passed via the stack, however in Win64 they are passed via the registers." He said it uses different instructions.. I guess thouh perhaps it's more than that, in the sense that if it's a different assembly language.. and assembly language is an obvious case of non portability.. hence I didn't mention it in the question, but it's good to see the examples at that link. And it's good knowledge to have. Good to see some contemporary assembly languages not obscure machines..