Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java completely Platform Independent?

Tags:

java

Is Java completely Platform Independent ?

if not then, what care needs to be taken to see that your code written in Java can run on Multi Platforms. Basically it should work on Targeted Platforms like Windows (Various versions), Linux (all flavors), Mac and Solaris.

like image 475
YoK Avatar asked Aug 06 '10 03:08

YoK


People also ask

Is Java platform independent or dependent?

The code is executed via the Java Virtual Machine (JVM). For the code to run, the JVM must exist. For each platform, a separate JVM exists. Java is a platform-independent language.

Is Java a platform independent language True or false?

Java is platform-independent but JVM is platform dependent In Java, the main point here is that the JVM depends on the operating system – so if you are running Mac OS X you will have a different JVM than if you are running Windows or some other operating system.

How Java is platform independent explain?

Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs.

Why Java is platform independent and secure?

Java is secure due to the following reasons: Java programs run inside a virtual machine which is known as a sandbox. Java does not support explicit pointer. Byte-code verifier checks the code fragments for illegal code that can violate access right to object.


2 Answers

While in practice, most compiled byte code is platform independent, my experience in my 12 years of developing on the Java platform has taught me that there are still idiosyncrasies from platform to platform.

For example, while developing a Java 1.4 Swing application for PC and MacOSX the behavior of dialogs was different if the parent frame is null.

Another example might be with working with the file system and files in general. The Java API has methods to help shield the developer from differences in path separators (/ vs \). When writing to a file, it important to use the FileWriter API as intended so that return characters and such are generated properly for the platform that it is being written on.

So while the motto is "write once, run anywhere" my experience has been for production envs it is write once, test, everywhere.

As a result, having strong unit and integration tests can help with this, as you can execute those tests on the various platforms you want to distribute your software.

Despite some minor issues here and there, it is cool to see your code running on Linux, Unix, Windows and MacOSX (BSD Unix) using the same JARs.

like image 143
Leo Avatar answered Oct 05 '22 22:10

Leo


As djacobson pointed out, the answer is a qualified "yes". For the most part, Java developers don't have to worry about platform dependencies. However, you may run into problems when you're dealing with APIs that handle traditional OS and platform functions.

When dealing with File I/O, for example, it's easy to make your code platform dependent by ignoring the differences between file/path separators across platforms (i.e. using '\' rather than File.separator).

like image 22
Aaron Novstrup Avatar answered Oct 05 '22 23:10

Aaron Novstrup