9

What is the meaning of platform independence?

I am taking the case of Java. Can I run a Java application on Linux that built on Windows platform? Or the reverse?

Can I use a same (or exact) programming algorithm on both operating systems?

In my view File Types are platform independent like Videos, Images, Documents etc.

1
  • 2
    Just a nitpick: most of the time, you can use the same algorithm in any language. Algorithm is about the idea of what you do, not about the actual code.
    – svick
    Jun 18, 2011 at 21:05

5 Answers 5

36

Platform independence in software means that you can run the same code with little or no modification on multiple platforms.

The devil is in the details:

  • It depends on what you define as "the platform". In some cases, this may be a specific hardware machine configuration. In other cases, it may be a "generic PC". In other cases, it may be a virtual machine and run time environment (which is the case with Java).
  • Nothing is "perfectly" platform-independent - there are always a few corner cases that can catch you out. For example, if you hard code file path separators rather than using the platform-independent File.pathSeparator in Java then your code won't work on both Windows and Linux. As a programmer, you need to watch out for these things, always using the platform-independent option where possible and test properly on different platforms if you care about portability.
  • There are always some constraints on specific platforms that cannot be ignored. Examples are things like the maximum length of filenames or the available RAM on a system. No matter how much you try to be platform-independent, your code may fail if you try to run it on a platform that is too tightly constrained.
  • It's important to note that some languages are platform-independent at the source code level (C/C++ is a good example) but lose platform independence once the code is compiled (since native code is platform-specific). Java retains platform independence even after code is compiled because it compiles to platform-independent bytecode (the actual conversion to native code is handled at a later time after the bytecode is loaded by the JVM).
  • There are occasional bugs in language implementations that only occur on certain platforms. So even if your code is theoretically 100% portable, you still need to test it on different platforms to make sure you aren't running into any unusual bugs!

In the specific case of Java:

  • Java code is platform-independent in the sense that the same Java application or algorithms (typically compiled to Java bytecode and packaged in a .jar file) will run identically on Windows and Linux.

  • Java libraries (e.g. all the nice open-source toolsets) are usually platform-independent, as long as they are written in pure Java. Most libraries try to stick with pure Java in order to maintain platform independence, but there are some cases where this is not possible (e.g. if the library needs to interface directly with a special hardware or call a C/C++ library that uses native code).

  • The Java platform /runtime environment is platform-independent in the sense that the same libraries (images, networking, File IO, etc.) are available and work in the same way on all platforms. This is done deliberately in order to allow applications that use these libraries to be able to run on any platform. For example, the Java libraries that access the filesystem know the fact that Windows and Linux use different filename path separators, and take account of this for you. Of course, this means that under the hood the run time environment does make use of platform-specific features, so you need a different JRE for each platform. You can see a list of some of the available platforms on the Java download site: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u26-download-400750.html

  • The JVM itself (i.e. the Java Virtual Machine that is responsible for JIT compiling and running Java bytecode) is platform-independent in the sense that it is available on many platforms (everything from mainframes to mobile phones). However specific versions of the JVM are needed for each underlying platform to take account of different native instruction codes and machine capabilities (so you can't run a Linux JVM on Windows and Vice Versa). The JVM is packaged as part of the Java platform/runtime environment as above.

Overall, Java is probably about as close to true platform independence as you can get, but as you can see there is still quite a bit of platform-specific work done under the hood.

If you stick to 100% pure Java code and libraries, my experience is that you can count on Java as being "effectively" platform-independent and it generally lives up to the Write Once Run Anywhere promise. But you should still test it!!

4
  • Apart from the technical info being good, this is also really well written. Really simple, in the same way that Hemingway writes on an elementary school level. I have no doubt even a complete non-programmer could understand this.
    – R. Schmitz
    May 9, 2019 at 10:15
  • in addition: you loose source-code-platform-independence if your app or lib has dependencies to platfrom specific stuff. Example: Android apps or libs written in java often need reference android specific stuff like Android-Context or Android-Activity which doe not existis outside of android so these are not portable.
    – k3b
    May 10, 2019 at 7:57
  • "if you hard code file path separators ... then your code won't work on both Windows and Linux" This isn't actually true. If you (masochistically) use backslashes as path separators it's not going to work in linux but if you use forward slashes, in most cases it will work fine in both Windows and Linux.
    – JimmyJames
    Dec 13, 2019 at 21:46
  • "The JVM itself (i.e. the Java Virtual Machine that is responsible for JIT compiling and running Java bytecode) is platform-independent in the sense that it is available on many platforms" ......the JVM is not platform independent. It is available for multiple platforms so it is platform dependent
    – pcodex
    Jan 13, 2021 at 20:05
5

You're right, platform independence means that the same program works on any platform (operating system) without needing any modification.

In the case of Java the application runs in a Java Virtual Machine which itself isn't platform independent. This has to be the interface between the actual machine (operating system) and the Java code you've written.

In the case of videos, images etc. these are documents and are data for applications so are usually platform independent by nature.

1
  • 1
    +1 for pointing out that the JVM isn't platform independent. In practice, that seems to mean Java is "write once, test everywhere". Jun 19, 2011 at 1:20
1

Actually the technique of achieving platform independence is

  • Building an intermediate platform
  • Implement the intermediate one in each one of the platforms you need (In languages like JAVA)

Or you can write code and compile for each platform (C/C++). In my opinion, this is also a kind of platform independence. And algorithms can be said to be platform independent in this way.

You are right in terms of true platform independence for file types. But the term platform independence is not usually applied to files.

1
  • +1 I want to add that compiling for each platform is called cross-compilation. This approach was also taken years ago by Borland in its Delphi product. They made another IDE called Kylix (which is Delphi for Linux) that allows sharing the same code base between Windows and Linux and compiling native applications for both platforms. They also did the same when they first released Delphi.Net. If you were careful enough you will be able to write code that can be compiled to native windows and .Net platforms.
    – M.Sameer
    Jun 18, 2011 at 21:03
0

JAVA is an object oriented platform independant programming language. The javac compiler compiles source code and produces Byte code language (universal language).

This language is not understandable by any operating system, it has to be processed first, and this is where a special executable program (JVM) enters the scene. An interpreter of the JVM reads the bytecode line by line and converts the byte code instructions into machine-specific understandable language (ultimately binary code). So, the byte code is platform independent but the interpreted code is machine-specific and will execute on the environment the JVM is installed on. JAVA programs are platform-independant means that JAVA is platform independant.

Sun MicroSystems slogan was WORA: Write Once Run Anywhere.

0
0

Since the JVM requires a host, I can tell you that a Java program is not platform independent. Also, the Java language by design has requirements on the filesystem resulting in short class names if the system is using a FAT16 partition:

class FOOBAR

is the same as

class Foobar

and the name

class AbstractPrinterFactory

is impossible.

As comparison, C has very little runtime requirements and can therefore offer higher portability. It is still possible to backport smaller C programs to really low-end devices.

Not the answer you're looking for? Browse other questions tagged or ask your own question.