Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

closed as too broad by gnat, MichaelT, david.pfx, Dynamic, jwenting May 19 at 7:17

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

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 '11 at 21:05

5 Answers 5

Platform independence in software means that you can run some 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 gases in may be a "generic PC". In other cases it may be a virtual machine and runtime 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 hardcode 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 use 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 langauges 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 occasionally 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 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 runtime 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 compililng 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!!

share|improve this answer

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.

share|improve this answer
1  
+1 for pointing out that the JVM isn't platform independent. In practice, that seems to mean Java is "write once, test everywhere". –  Mike Sherrill 'Cat Recall' Jun 19 '11 at 1:20

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.

share|improve this answer
    
+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 '11 at 21:03

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.

share|improve this answer

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.

share|improve this answer

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