81

I just started learning Java and I'm confused about the topic of platform independence.

Doesn't "independent" imply that Java code should run on any machine and need no special software to be installed? Yet the JVM needs to be present in the machine.

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

Could somebody please what is meant when Java is described as "platform independent"?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Serenity
  • 4,968
  • 19
  • 65
  • 104
  • 4
    @aaa: Last I checked, it was available for free download on Borland's website. – dan04 May 01 '10 at 04:44
  • 1
    Yes, listed as Antique Software... (http://edn.embarcadero.com/article/20841). – Matthew Flaschen May 01 '10 at 04:49
  • 2
    Turbo C (or any other 'native' compiler) is not needed to execute the compiled program. the compiler generates an `.exe` file. at most it requires a runtime library (in a `.dll` file), not the whole compiler – Javier May 01 '10 at 05:10

24 Answers24

114

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

Tharif
  • 13,794
  • 9
  • 55
  • 77
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 1
    "Since all the JVMs work exactly the same, the same code works exactly the same everywhere without recompiling". What does this sentence mean? Whenever you run the same program on different systems having JVM installed we always do this "javac filename.java". (Take any simple program for example) If it is not required to recompile the program again then why it is said that "we could run the same code anywhere without recompiling?". Please explain. – Karan Thakkar Apr 17 '16 at 14:05
  • 3
    You need to compile java source code, hence the "javac filename.java" However, you only need to once, and not on every machine where it is used (this differs from C and C++). If you change CPUs, 32 / 64 bit systems, byte orders, etc, the code still runs. This is because the output "filename.class" contains byte code, which are instructions to the JVM. Since all JVMs are functionally identical, once code is compiled, you can then run that bytecode on any Java Virtual Machine without having the source code. Try it, it works. Also, some comments you make confuse compiling with recompiling. – Edwin Buck Apr 17 '16 at 19:53
  • @KaranThakkar - I think an extension of what Edwin meant to say could be, easily being able to use the same .jar files in your projects on any platfrom without compiling them. – vanguard69 Sep 26 '16 at 06:36
54
            Technical Article on How java is platform indepedent?

Before going into the detail,first you have to understand what is the mean of platform? Platform consists of the computer hardware(mainly architecture of the microprocessor) and OS. Platform=hardware+Operating System

Anything that is platform indepedent can run on any operating system and hardware.

Java is platform indepedent so java can run on any operating system and hardware. Now question is how is it platform independent?

This is because of the magic of Byte Code which is OS indepedent. When java compiler compiles any code then it generates the byte code not the machine native code(unlike C compiler). Now this byte code needs an interpreter to execute on a machine. This interpreter is JVM. So JVM reads that byte code(that is machine indepedent) amd execute it. Different JVM is designed for different OS and byte code is able to run on different OS.

In case of C or C++(language that are not platform indepedent) compiler generate the .exe file that is OS depedent so when we run this .exe file on another OS it will not run because this file is OS depedent so is not compatible with the another OS.

Finally an intermediate OS indepedent Byte code make the java platform independent.

Jatin Khurana
  • 1,155
  • 8
  • 15
  • 5
    This is a stupid answer. What makes java platform independent is the fact, that java is VM dependent. It dosen't cares what OS is it running on as long as that VM satisfies the calls to OS operations. And VM by itself is not platform independent. The comparison you provided with C/C++ language was bogus, just compile the source code on that very platform and it will run, why taking that set of instructions to different platform and expecting foolishly that it'll run? Can you take your java source code to ARM and expect it to run on VM compiled for my custom_processor? – Abhinav Gauniyal Jan 28 '16 at 09:12
  • The design for a JVM is the same. Oracle will go nuts designing different JVMs for each and every machine. Only difference is the bytecode of the program you want to run on some machine. Each program generates a different type of bytecode. The bytecode file is of hexadecimal format for the JVM and CPU to understand and execute the instruction set. – Karan Thakkar Apr 17 '16 at 13:56
25

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 modifying or recompiling.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 10
    "... 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 May 01 '10 at 05:27
  • @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 May 01 '10 at 05:45
  • 1
    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 May 01 '10 at 06:04
  • agreed ... but in none of those cases will recompiling the "Java binaries (bytecode)" make the slightest difference!! – Stephen C May 01 '10 at 06:14
  • I have one question, does the complied Java bytecode interact with the OS kernel? If so, the same bytecode cannot be applied to different kernels since the Kernel functions are different, can it? Or the JVM contains all the kernel functions for every different kernels, which is very unbelievable... – henryyao Jun 09 '13 at 01:36
  • @henryyao, no, the bytecode is not written for a real machine, and does not talk directly to a kernel. In a simple (not modern) JVM implementation, the JVM is simply interpreted one step at a time. In a modern one, it is recompiled to machine code, and *that* latter machine code runs on the CPU directly (and possibly uses kernel interrupts, not sure). – Matthew Flaschen Jun 10 '13 at 20:25
8

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
  • 8,601
  • 7
  • 41
  • 56
8

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
  • 687,336
  • 108
  • 737
  • 1,005
5

Java is platform-independent as it has JVM(Java virtual machine). Let us illustrate it with a real life example. Let's assume you are free to your family members. But why?

Because you know them well and they know you as well. But, you are not free to my family members. Because you don't know them and they don't know you either. But, if I'm your friend and when I can introduce you to my family members, hence you will be able to talk to them freely.

In a similar way, if you are a code and I am a JVM. Also, your family is windows platform and mine is the Linux platform. In the case you were a C or other platform-dependent languages, you only know your family members and vice versa. That's why only the platform on which you were written knows that Code and will support it. But if you are a JAVA code and when you come to my family viz. the Linux platform and if there you find me, JVM, then I can introduce you to my family, the Linux platform and you will be able to interact with it.

For platform-dependent languages, there isn't any friend like JVM available to them to introduce themselves to any platform family. That is how Java is platform-independent. :)

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Tazwar Utshas
  • 921
  • 2
  • 17
  • 30
3

java is not platform Independent, itself is a platform,based on that platform java apps runs,but java platform itself is platform dependent

3

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
  • 89,107
  • 111
  • 320
  • 448
3

In c/c++ the source code(c program file) after the compilation using a compiler is directly converted to native machine code(which is understandable to particular machine on which u compiling the code). And hence the compiled code of c/c++ can not run on different OS.

But in case of Java : the source file of java(.java) will be compiled using JAVAC compiler(present in JDK) which provides the Byte code(.class file) which is understandable to any JVM installed on any OS(Physical System).

Here we need to have different JVM (which is platform dependent) for different operating Systems where we want to run the code, but the .class file(compiled code/Intermediate code)remains same, because it is understandable to any of the JVM installed on any OS.

In c/c++ : only source code is machine independent. In Java : both the source code and the compiled code is platform independent.

This makes Java Platform(machine) independent.

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
2

1:jvm(i.e java virtual machine)is a collection of programs which contains lot of files which provides various functionatiles present on a folder(i.e collections of programs on middle level format)as called packages.jvm helps not to be overloaded on o/s where its helps to execute only the .class files or java applications only by itself only.It helps to make its equalities middle level format after compliation by the java compiler then its provide the byte code (.class file)reprsentation which is not specific to o/s and processor .
2:jvm makes byte code to .exe file for proccessor to understandable and prsents memory allocation for every functions after recieving frm byte code.
3:jvm also releases memory alocation from ram after control makes finishes thier execution .

bipen
  • 36,319
  • 9
  • 49
  • 62
ranjan
  • 21
  • 1
2

JVM is os dependent. for every os JVM different.

".class" is same for all JVMs. so, every JVM understand that ".class" file data.

windows dependent JVM give windows dependent instruction to windows linux dependent JVM give linux dependent instruction to linux.

that's like it for other operating systems. so,java runs on any operating system.

that's why java is os independent.

time pass
  • 21
  • 1
2

In simple terms:

Java programming language is platform independent.

JVM is platform dependent

Mohan
  • 334
  • 2
  • 12
1

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
  • 11,395
  • 2
  • 29
  • 38
1

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
  • 562,542
  • 136
  • 1,062
  • 1,124
1

JVM will be platform dependent.
But whatever it will generate that will be platform independent. [which we called as bytecode or simply you can say...the class file]. for that why Java is called Platform independent.
you can run the same class file on Mac as well on Windows but it will require JRE.

Nitz
  • 1,690
  • 11
  • 36
  • 56
0

bytecode is not plateform independent, but its JVM that makes bytecode independent. Bytecode is not the matchine code. bytecodes are compact numeric codes, constants, and references (normally numeric addresses) which encode the result of parsing and semantic analysis of things like type, scope, and nesting depths of program objects. They therefore allow much better performance than direct interpretation of source code. bytecode needs to be interpreted before execution which is always done by JVM interpreter.

ScoRpion
  • 11,364
  • 24
  • 66
  • 89
0

Just a side note to the discussion about JVM and JIT Compilation. This is the same principle as with C# and the CLR and to some extent in Python, and when somebody says that the code runs "directly on hardware" that is actually true in that instructions that is already compiled will be able to take advantage of optimization on the machine/cpu it's being run on. So even if the initial compilation of a module is rather slow, the next time this module is run, the code being executed runs at native speed and thus runs directly on hardware so to say.

0

Java is platform independent in aspect of java developer,but this is not the case for the end-user, who need to have platform dependent JVM to run java code. Basically, when java code is compiled, a bytecode is generated which is typically platform independent. Thus, the developer has to have write a single code for entire platform series. But, this benefit comes with a headache for end-user who need to install JVM in order to run this compiled code. This JVM is differnt for every platform. Thus, dependency comes into effect only for end-user.

0

Javac – compiler that converts source code to byte code. JVM- interpreter that converts byte code to machine language code.

As we know java is both compile**r & **interpreter based language. Once the java code also known as source code is compiled, it gets converted to native code known as BYTE CODE which is portable & can be easily executed on all operating systems. Byte code generated is basically represented in hexa decimal format. This format is same on every platform be it Solaris work station or Macintosh, windows or Linux. After compilation, the interpreter reads the generated byte code & translates it according to the host machine. . Byte code is interpreted by Java Virtual Machine which is available with all the operating systems we install. so to port Java programs to a new platform all that is required is to port the interpreter and some of the library routines.

Hope it helps!!!

0

When we compile C source data, it generate native code which can be understood by current operating system. When we move this source code to the other operating system, it can't be understood by operating system because of native code that means representation is change from O.S to O.S. So C or C++ are platform dependent .

Now in case of java, after compilation we get byte code instead of native code. When we will run the byte code, it is converted into native code with the help of JVM and then it will be executed.

So Java is platform independent and C or C++ is not platform independent.

Monis Majeed
  • 1,358
  • 14
  • 21
0

{App1(Java code)------>App1byteCode}........{(JVM+MacOS) help work with App1,App2,App3}

{App2(Java Code)----->App2byteCode}........{(JVM+LinuxOS) help work with App1,App2,App3}

{App3(Java Code)----->App3byteCode}........{(JVM+WindowsOS) help work with App1,App2,App3}

How this is happening ?

Ans: JVM have capability to read ByteCode and Response In Accordance with the underlying OS As the JVM is in Sync with OS.

So we find, we need JVM with Sync with Platform.

But the main thing is that the programmer dont have to know specific knowledge of the Platform and program his application keeping one specific platform in mind.

This Flexibility of write Program in Java --- compile to ByteCode and run on any Machine (Yes need to have Platform DEPENDENT JVM to execute it) makes Java Platform Independent.

Garg
  • 2,731
  • 2
  • 36
  • 47
RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
0

well good question but when the source code is changed into intermediate native byte code by a compiler in which it converts the program into the byte code by giving the errors after the whole checking at once (if found) and then the program needs a interpreter which would check the program line by line and directly change it into machine code or object code and each operating system by default cannot have an java interpreter because of some security reasons so you need to have jvm at any cost to run it in that different O.S platform independence as you said here means that the program can be run in any os like unix, mac, linux, windows, etc but this does not mean that each and every os will be able to run the codes without a jvm which saysspecification, implementation, and instance , if i advance then by changing the configuration of your pc so that you can have a class loader that can open even the byte code then also you can execute java byte code, applets, etc. -by nimish :) best of luck

-1

when we compile java file the .class file of that program is generated that .class file contain the byte code.That byte code is platform independent,byte code can run on any operating system using java virtual machine. platform independence not about only operating system it also about the hardware. When you run you java application on a 16-bit machine that you made on 32-bit, you do not have to worry about converting the data types according to the target system. You can run your app on any architecture and you will get the same result in each.

-3

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.

Amy B
  • 17,874
  • 12
  • 64
  • 83