Why is the compiler platform-independent, while JVM is platform-dependent?

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
[199 byte] By [java80a] at [2007-10-2 7:17:26]
# 1
The compiler can be platform independent, because it can be written in Java. The VM must have at least some parts that are platform-specific (I/O, GUI, ...).
jverda at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 2
If by "compiler" you mean "JAVA language" (which is not the same thing, BTW), it is because a compiler takes the JAVA source code and creates JAVA bytecode with it, which is platform-independent...The JVM then interprets the bytecode and executes it... :)
vagrantcharlya at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 3

> 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

JosAHa at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 4
> Try downloading and running any binary executable for a platform X on a platform Y. It simply won't work.Although with programs like Wine it's hard to say precisely what a platform is.
YAT_Archivista at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 5

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.

LinuxNewba at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 6

> 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. -:)

LinuxNewba at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 7

> 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.

LinuxNewba at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 8

> 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)

Java_interview_questiona at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 9

> 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.

Java_interview_questiona at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 10
> In reality this includes all modern JVM's and this> means that for all practical purposes Java today is a> compiled language.Well it always was compiled (to bytecode) but I mean natively compiled (to the machine language on the target platform).
Java_interview_questiona at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 11

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

javedka at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 12
Why did you resurrect this thread after over a year simply to repeat what's already been said?
jverda at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...
# 13
> Why did you resurrect this thread after over a year> simply to repeat what's already been said?Why did you ask why? ;)
cotton.ma at 2007-7-16 20:52:34 > top of Java-index,Java Essentials,New To Java...