> Hi Friends,
> Can someone please answer me this interview question i found on techinterview:
> Q.Why is the compiler platform-independent, while JVM is platform-dependent?
> Thanks
Try downloading and running a JVM meant for Solaris on a windows
box. Try downloading and running any binary executable for a platform
X on a platform Y. It simply won't work.
kind regards,
Jos
Programs like Wine may blur the line a little, but Wine is no substitute for native machine code execution speed. (Try playing your favorite fps game in Wine...I know personally that World of Warcraft (not technically a fps game, but you get my drift) plays at a shabby 18-22 fps (all graphics options turned down and low resolution) on Wine (Gentoo Linux with a GeForce 6600 with a glxgears fps of about 330 in fullscreen mode) on a machine that runs World of Warcraft well over 30 fps (Win2k) with all the graphics options enabled in 1280x1024 resolution).
The point is that emulators have their place, and that is code portability. The Java compiler takes java source and compiles it to a "middle-state" called byte-code that is platform independent. The JVM takes this byte code and converts it to native machine code which is then executed. The beauty of this is that you can compile the source code into byte code on a Solaris, then take that byte code and execute it on a Windows or Linux (or whatever) machine. The only cost is the time it takes to convert from byte code to native machine code.
So, with that out of the way, the compiler *output* is platform independent because it is intended to run ONLY on the JVM, and it's up to the JVM to convert byte code to machine code for it's specific platform.
I personally would like to see a Java "co-processor" hardware setup that would allow the byte code to execute directly in hardware...that would be nice.
> Can someone please answer me this interview question
> i found on techinterview:
> Q.Why is the compiler platform-independent, while JVM
> is platform-dependent?
Remember that platform independence first and foremost is a Sun marketing term. According to this the JVM is defined to be the platform dependent part which allows programs written in pure Java to be platform independent. It's the egg that comes before then hen. So the JVM is defined to be platform dependent. Everything else is platform independent if and only if it's written in pure Java.
The question then becomes. What do you call a JVM that's written in pure Java? Well maybe you could ask the interviewer. -:)
> The question then becomes. What do you call a JVM
> that's written in pure Java? Well maybe you could ask
> the interviewer. -:)
My answer would be that it's a question of definition. A JVM written in pure Java would lack the ability of making Java programs platform independent. If you require that of a JVM then such a JVM isn't a JVM.
The proper name probably would be a JVM emulator. I think IBM has developed one actually.
> Hi Friends,
> Can someone please answer me this interview question
> i found on techinterview:
> Q.Why is the compiler platform-independent, while JVM
> is platform-dependent?
> Thanks
Because the Java Virtual Machine is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris , Linux, or MacOS. Some virtual machines, such as the Java HotSpot Virtual Machine , perform additional steps at runtime to give your application a performance boost. This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently-used sections of your code.
a.java --> compiler --> a.class [class files do not have code native to your processor, it instead contain byte codes]
a.class --> Interpreter JVM for solaris --> Solaris
a.class --> Interpreter JVM for Windows --> Windows and son
cheers
Arul (http://www.lulu.com/content/192463)
> Some virtual machines,
> such as the Java HotSpot Virtual Machine , perform
> additional steps at runtime to give your application
> a performance boost.
In reality this includes all modern JVM's and this means that for all practical purposes Java today is a compiled language.
Is JVM platform dependent?
Although java is platform independent but still JVM IS platform dependent one of the feature called byte code makes JVM platform dependent. Byte code is an intermediate machine code of compiled source code. The byte code can run on all machines, however the JVM must be installed in each machine