aspose file tools*
The moose likes Java in General and the fly likes how java is platform independent? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how java is platform independent?" Watch "how java is platform independent?" New topic
Author

how java is platform independent?

abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42
i have a big confusion...if we talk about platform independence, then this is achieved through combination of byte code mechanism and jvm. if we have jvm for any machine than any bytecode will run on that machine irrespective of the fact that on which machine the source code was compiled. as far as programmers are concerned then they dont have to worry about all this, they will simply write and compile.they dont have to think abt specific platform...so isn't the same thing applicable for c/c++ as well, we write our source code and if we have compiler for a given machine than we will just compile it and then use it. almost same thing as java mechanism. even in this case programmers don't have to worry about the platform on which the program is going to run, that issue will be tackled by the compiler for that platform. so how c/c++ is not platform independent??? is dere something like we have a little different language features for different platforms??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Java *byte code* will run on any platform. Compiled C code won't.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
Java code gets compiled to one machine only, the JVM. C/C++ gets compiled to the machine the complier was built for (example x86). So each change in architecture requires a new compiler for that architecture or the C/C++ program can't be made to run.

On the other hand, Java can not run on a machine that doesn't have a JVM. For Java it isn't the compiler that is needed for the new architecture, it's the JVM. If you don't have a JVM for a particular architecture then you can't run a Java application for it.
abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42
ya dats true that compiled code of c/c++ won't(or may not) run on other machines, but i am talking about the independence which we have as a programmer..because we don't have to think about platform while we are writing a program because that gets handled by the compiler of that particular machine..so isnt in this way java and c++ are same...programmers don't have to think abt anything in both the cases....may be one thing, that we are considering platform independence in terms of write here,compile here and then run everywhere..not like write here ,compile here, run here , then for running on the other machine , compile and then run...platform independence is considered in perspective of what?? independence for the programmers for writing the programs without considering any platform or independence of running same code everywhere without recompiling it....???
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

GUI platforms are different, too.

In any case, you're missing the point: Compiled Java code is cross-platform. Compiled C code isn't.

(And you're mistaken in believing that writing Unix C code is the same as Windows C code, unless you're running under Cygwin, or writing very simplistic, non-threaded programs. Without the assistance of macros it's not cross-platform, even across Unix variants.)
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
I'll agree with David, GUIs are a big differentiator for Java. Build something simple and run it on Mac, Windows and Linux and you'll see what I mean.

Serial port communication is also different. Windows will report Comm while Linux will show TTYs.

But once this is taken into consideration and a jar or war file are built, they run on all platforms that have a JVM. That one particular JAR or WAR fits multiple platforms. You can carry it on a memory stick and run it. With other languages you need to carry many executables if it were done with C.
abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42
i didn't say that compiled c code is cross platform ...ya now i am getting it...so i was under wrong impression that unix c code is not different from windows code..so can you give me some more examples where difference comes into existence...by the way thanks to both of you !!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Download almost any cross-platform, open-source C program: if it's not Cygwin-based you'll see pre-processor macros. Even Unix-only C/C++-based programs will usually have a plethora of macros to control compilation, particularly across BSD/AT&T variants.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

abhishek kunal wrote:so can you give me some more examples where difference comes into existence...

The API for Windows is totally different than the API for Linux, and the API for Mac OS X is totally different again. When you write a C or C++ program that uses the Windows API, then your source code will be largely useless on a Linux system or on a Mac - because the Windows API doesn't exist there.

Java, on the other hand, has a standard library and API that is the same on any platform.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42
great!! ohhh it was a little(even more) silly from, API will varry greatly in accordance with the platform..in this case the code won't even compile...now i can say java is totally platform independent but c/c++ are not by any means!! thank you soooo much!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how java is platform independent?