Solved

what makes java platform independent

Posted on 2004-04-28
13
1,089 Views
Last Modified: 2006-11-17
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.

regards

nikhil
0
Comment
Question by:nikhilbansal
  • 4
  • 4
  • 2
  • +3
13 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 10946657
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
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10946694
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.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10947430
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.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10948364
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!


;JOOP!
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10948463
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.
0
 
LVL 1

Author Comment

by:nikhilbansal
ID: 10976637
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.

Regards

Nikhil

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:JakobA
ID: 10976966
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
0
 
LVL 1

Author Comment

by:nikhilbansal
ID: 11012576
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.

Regards

nikhilbansal
0
 
LVL 15

Accepted Solution

by:
JakobA earned 50 total points
ID: 11012763
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
0
 
LVL 1

Author Comment

by:nikhilbansal
ID: 11012995
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.

regards
0
 
LVL 15

Expert Comment

by:JakobA
ID: 11013079
better not, emailing me is useless as I am drowning in SPAM and will probably overlook your mails. Just ask questions here.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11013685
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.
0
 
LVL 1

Author Comment

by:nikhilbansal
ID: 11013897
k

tht's fine guys

regards

nikhilbansal
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 53
topping3 challenge 14 52
wordappend challenge 8 88
maven example error 3 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now