what makes java platform independent

Can someone explain me what makes Java a platform independent language.

can't we say that other languages like C or C++ are also platform independent.

I know tht source code of a  java program gets compiled into a bytecode. but say if we implement the same feature in C or C++ won't they be platform independent.

i am really confused abt this platform independent thing.

someone plz throw light on this.


Who is Participating?
JakobAConnect With a Mentor Commented:
The Java language is quite young, and still developing, so there are different versions of the VM around. The rule is to get the latest version, it will (almost always) run Java programs correctly, even if they were compiled with an 'older' Java compiler.

If you have a Windows PC with Explorer browser You get at VM made by microsoft along with it, and that is NOT the newest version. It also do not quite comply with the Java standards. I recommend you replace it with the 'proper' VM from sun. It is possible to write Java programs that can run on the microsoft VM and cannot run on the Sun VM. (The autentication code from my internet Bank is one, So I have had to add a bootpartition with the microsoft-VM on my computer).

The theory is clear and clean, but reality (as usual) is a bit of a mess :-))

regards JakobA
Actually I do not relly subscribe to the wiew that Java is platform independent. Only thet it is MORE platform independent than C, Pascal and all the other older languages.

In the motorola family of processing chips a number is stored as a sequence of bytes. a 16bit value is stored with the most significant bits in the byte with the lowest adress
In the Intel family it is the other way around. the most significant bits are stored in the byte with the highest adress.
There was even a CPU chip (the 16016 from National) that mixed it so that datavalues vere stored the motorola way, and memory adresses was stored the intel way.

So for each of these processing units tha compiler will have to comepile the program you write to something different in order to get data stored right.

The number of bytes used in a calculation can differ. in a 32-bit CPU numbers stored in 4 bytes can be added with at single instruction.
       ADD   accumulator, #address                  ;add value at £address to accumulator
in a 15 bit CPU the same operation require 2 (or more) instructions
       ADD   accumulator.low, #address            ;add lowest 16 bit
       ADC   accumulator.high, #address+2        ;add hightest 16 bit (with carry)
Note that abowe I have assumed the CPU is of the Intel family. with the highest part of the 32-bit value at the highest address.

One way many programming languages compensate for such differences is to adjust the length of the standard datatypes to the length of the words you can use with the cpu you are writing a compiler for. In Pascal (a fairly old language) a wariable declared as an integer
   var   my_variable : integer;
can only contain values small enoughg to be stored as a 16-bit signed number.

if you say
   a := 32000;
   a := a + 32000;
the program will crash when 32000 is added to 32000 (there is not enough room for the result)

There are LOTS of that sort of differences. and they wil result in programs that work on one computer to go horribly bad if you try to run the same program on another computer.

So Java have found standardized solutions to a lot of thos problems. It makes it much easier to write programs that work on all computers without needing to be rewritten

regards JakobA
It's like JakobA says, and remember that the Java.exes are different for each platform - it's really them that do the work of deciding the correct bytecodes, so it's really smoke and mirrors - it looks platform independent, but really the code is being custom interpreted for each OS.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Hi all,

All java source files are precompiled as standard java bytecode : it's the same for all platforms, you only need a platform dependent java interpreter.
Other languages are less platform independent: the compiled code will not work on other platforms, but are executed faster than interpreted precompiled code.
Have you ever written a program in C or COBOL on one system and even tried to compile it elsewhere?

OK, I write programs in JAVA and I don't give a ... on what system and for what target.

That's it!

Mayank SAssociate Director - Product EngineeringCommented:
Exactly - the magic is the bytecode - which is same everywhere. All the JVMs will interpret the same bytecode, though the JVMs would in themselves be different. However, whatever platform you're working on, you would download the appropriate JVM for that platform, hence it would always do the work for you.

The hidden fact here is that - Sun has provided the JVM for all platforms, be it Windows, UNIX, Mac, etc. You have to give credit to the hard-work they've done by trying to bring out the concept of an intermediate optimized bytecode and the concept of different JVMs for different platforms. So what if the java.exe would be different? It doesn't matter because you would use the java.exe which is appropriate for the platform on which you are working.

>> say if we implement the same feature in C or C++ won't they be platform independent.

As of now, they are not. The question is *if* you do it for them. .NET has tried to implement exactly the same things which Java has. They have succeeded in making something called as IL code (intermediate language) which would be the same, whether the code is written in C++, C#, VB, ASP, etc. It would be compiled to IL using a language-specific compiler and then the .NET compiler and CLR (common library runtime) would perform the second step. It is not interpreted, though. But the disadvantage is that, as of now, the CLR provided by M$ is only for Windows. They have left it as an 'open system' for others to implement if they wish to, for other operating systems. They have just provided the framework (which is essentially all imitated ;-) ). And I don't think that they would provide any other implemention for any other OS either.

I find it really easy to develop J2EE applications on Websphere (for Windows) and then deploy the EAR on a Websphere server running on UNIX. UNIX/ Solaris servers are definitely much faster than NT servers.

Try writing:

#include <conio.h>
clrscr () ;

- on Turbo C++. It will work. Run it on Visual C++. It will not run. Then you will know what is the advantage of writing code only once, which can be ported to any platform.
nikhilbansalAuthor Commented:
Dear All

Thx for providing the inputs.

I feel tht Jakoba is close to answering my question.

Jakoba could you elaborate more on this statement "So Java have found standardized solutions to a lot of thos problems." i.e. i wish to know what are the standardized solutions.



The main solution is the 'bytecodes' idea, that kraratoa points out. Rather than letting Java programs compile to the processor instructions used on the computer they compile the program to a particilar set of processor instructions the people at sun have defined. You could say they have defined their own (non-existing) processing unit; a Virtual Machine (I'm sure you have heard of the Java VM)

Then for each kind of computer they write a program that will execute the bytecodes on that particular computer, no matter how long its words are or how they are stored, etc.
The Java VM is implemented again and again, and quite diffent in a Windows PC or a Mac or a SparcStation, etc. But on whichever computer it will give the same results when running bytecodes generated by any Java compiler.

It is not really a new Idea. The Pascal Language tried the same idea in ca 1970 (they called it P-code). But Java does it more thoroughly and faster.

regards JakobA
nikhilbansalAuthor Commented:
Dear Jakoba

I think u have answered the question very well.

I just have one doubt. U say tht it is the duty of the JVM to translate the bytecode to m/c specific code. Now there are diff JVM's for each OS. does tht mean tht i have to be bothered abt which JVM to be implemented for tht particular OS.


nikhilbansalAuthor Commented:
dear jakoba

thx a lot for providing inputs.

well i am nikhil bansal, working in a software company in india.i'll be working on java platform in near future. can i have ur email id, for i may need some help.

better not, emailing me is useless as I am drowning in SPAM and will probably overlook your mails. Just ask questions here.
Mayank SAssociate Director - Product EngineeringCommented:
Moderators also don't like seeing e-mail IDs on pages here because its against policies.

All questions should be solved here so that everyone can see it.
nikhilbansalAuthor Commented:

tht's fine guys


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.