tags:

views:

319

answers:

9

Just started learning Java and I am confused about this whole independent platform thingy.

Doesn't independent means that Java code should be able to run on any machine and would need no special software to be installed (JVM in this case has to be present in the machine)?

Like, for example, we need to have Turbo C Compiler in order to compile C/C++ source code and then execute it.. The machine has to have the C compiler.

guess I am confused..Somebody please explain in simple language or may be direct me to a tutorial that explain things in simple language ? that would be great

I am just not getting the concept.

+9  A: 

It means the Java programmer does not (in theory) need to know machine or OS details. These details do exist and the JVM and class libraries handle them. Further, in sharp contrast to C, Java binaries (bytecode) can often be moved to entirely different systems without recompiling.

Matthew Flaschen
"... Java binaries (bytecode) can often be moved to entirely different architectures without recompiling.". Actually, they *always* can. Recompilation is only necessary when moving code compiled for a newer JVM to an older one.
Stephen C
@Stephen, I changed it to "systems." There are many factors that can impede portability, including using JNA or JNI to link to libraries that are only written for a single architecture.
Matthew Flaschen
JNA or JNI would seem to be the only factor to me, and those are pretty rare in real world use. Even then, the bytecode can still always be moved without recompiling - it's just you may have to also re-compile the libraries the JNI is expecting to be there. The byte code is still portable though.
Kendall Helmstetter Gelner
@Matthew - agreed ... but in none of those cases will recompiling the "Java binaries (bytecode)" make the slightest difference!!
Stephen C
+5  A: 

The JVM is a "simulated machine" that can be installed on different systems. In this way, the same Java code can run on different systems, because it relies on the JVM, not on the operational system itself.

That is to say, this allows the programmer to communicate with the virtual system (JVM) and utilize its functions, instead of the specific machine and OS functions. Since Java only relies on JVM, it is platform independent (if the platform has JVM installed).

So in short, Java is not platform independent as such, it requires a JVM-installation for all systems it should run on. However, it will run on all systems that has the JVM installed.

Lars Andren
A: 

Edit: Not quite. See comments below.

Java doesn't directly run on anything. It needs to be converted to bytecode by a JVM.

Because JVMs exist for all major platforms, this makes Java platform-independent THROUGH the JVM.

Coronatus
The JVM interprets or JITs bytecode. It doesn't compile to it.
Matthew Flaschen
Yes. "for the JVM" is more accurate.
polygenelubricants
http://www.jopdesign.com/ ~ might try that again. these guys say it can run directly on hardware.
drachenstern
+3  A: 

No, it's the other way around. It's because you use the virtual machine that the Java program gets independend.

The virtual machine is not independent, you have to install one that is specifically made for your type of system. The virtual machine creates an independent platform on top of the operating system.

Guffa
+2  A: 

The JVM abstracts from the concrete platform. Your program relies only upon the JVM and since the JVM is available for different platforms like Windows and Linux, your program is platform independent (but jvm depended).

deamon
+1  A: 

Java is not platform independent in that it runs on the JVM. Having said that, you gain platform independence via programming against a single abstract machine that has concrete realizations on most common OS platforms (and some embedded appliances).

A related idea is the hardware abstraction layer present in many operating systems that allows the same OS to run on disparate hardware.

In you original question, Turbo C is analagous to the javac program, and the JVM is the OS/HAL.

John Percival Hackworth
A: 

Doesn't independent means that Java code should be able to run on any machine and would need no special software to be installed (JVM in this case has to be present in the machine)?

With Java, you can compile source code on Windows and the compiled code (bytecode to be precise) can be executed (interpreted) on any platform running a JVM. So yes you need a JVM but the JVM can run any compiled code, the compiled code is platform independent.

In other words, you have both portability of source code and portability of compiled code.

Like, for example, we need to have Turbo C Compiler in order to compile C/C++ source code and then execute it.. The machine has to have the C compiler.

The machine doesn't have to have a C compiler, the machine has to use a platform specific binary. With C or C++, the compiled code is specific to each architecture, it is platform independent.

In other words, with C / C++ you have portability of source code (with some discipline) but not portability of compiled code: you need to recompile for each architecture into platform specific binaries.

Pascal Thivent
+2  A: 

Typically, the compiled code is the exact set of instructions the CPU requires to "execute" the program. In Java, the compiled code is an exact set of instructions for a "virtual CPU" which is required to work the same on every physical machine.

So, in a sense, the designers of the Java language decided that the language and the compiled code was going to be platform independent, but since the code eventually has to run on a physical platform, they opted to put all the platform dependent code in the JVM.

This requirement for a JVM is in contrast to your Turbo C example. With Turbo C, the compiler will produce platform dependent code, and there is no need for a JVM work-alike because the compiled Turbo C program can be executed by the CPU directly.

With Java, the CPU executes the JVM, which is platform dependent. This running JVM then executes the Java bytecode which is platform independent, provided that you have a JVM availble for it to execute upon. You might say that writing Java code, you don't program for the code to be executed on the physical machine, you write the code to be executed on the Java Virtual Machine.

The only way that all this Java bytecode works on all Java virtual machines is that a rather strict standard has been written for how Java virtual machines work. This means that no matter what physical platform you are using, the part where the Java bytecode interfaces with the JVM is guaranteed to work only one way. Since all the JVMs work exactly the same, the same code works exactly the same everywhere without recompiling. If you can't pass the tests to make sure it's the same, you're not allowed to call your virtual machine a "Java virtual machine".

Of course, there are ways that you can break the portability of a Java program. You could write a program that looks for files only found on one operating system (cmd.exe for example). You could use JNI, which effectively allows you to put compiled C or C++ code into a class. You could use conventions that only work for a certain operating system (like assuming ":" separates directories). But you are guaranteed to never have to recompile your program for a different machine unless you're doing something really special (like JNI).

Edwin Buck
+1  A: 
Nitz