Java is a framework language. This means that once your code is written, you may run it anywhere, on any platform that has the necessary environment. 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. Platform independence does not imply that Java code may be executed everywhere; rather, it refers to the ability to execute Java code in any environment.
Java is one of the most popular programming languages for web developers and the capacity to provide distinctive features accounts for the huge response. Although Java's syntax is similar to C++'s, it provides many more functionalities. Both are object-oriented programming languages.
How Does a Program Execute In JAVA?
The javac executes any Java code which is written.
- Instead of machine native code, the JAVA compiler produces a .class file or bytecode (unlike the C compiler).
- The resulting bytecode is not machine-executable and requires an interpreter to run on a computer. The JVM serves as this interpreter, and as a result, it executes the Bytecode.
- The software then eventually executes to get the intended result.
The compiler creates an OS-dependent.exe file when C or C++ is used (these languages are not platform independent). This.exe file won't run when we try to run it on a different OS since it depends on that OS and isn't compatible with it.
How is JAVA Platform Independent?
Now that we've discussed high-level program execution in it. Let's make it independent of the platform. Unlike other programming languages, the bytecode produced by the javac compiler may be run on a variety of Operating Systems. In fact, Java's produced bytecode simply requires the JVM. The operating system has no impact on it. You can write and execute the code on any platform as long as the javac compiler is present.
How is a JAVA Code Executed?
In Java, the compiler converts the program into bytecode, which is not executable code. Now, any other platform or operating system may run this bytecode. When we execute its program or file, we obtain a .class file, which is a non-executable file. We would require a virtual machine to execute a .class file. JVM is undoubtedly platform-independent. Similar to how it varies depending on the surroundings. For instance, MAC and Linux have a different JVM than Windows. It supports portability as one of its key features since its code's portable bytecode makes it platform-independent.
What Does Platform Independence Mean?
A platform is a software or hardware on which a program operates regularly. A platform for this language, the Java Runtime Environment, is available. Platform independence refers to the program's independence from the system's operating system. It adopts the "Write Once Run Anywhere (WORA)" methodology.
Compiling a Code
Every time we write code, we adhere to a programming language's syntax, which is generally readable. A few words, variable names, phrases, method names, etc., are included. All of them are regarded as literature that humans can read and understand.
As a result, a computer runs a compiler to turn source code into machine-readable code, also known as machine-level language. The compiler creates the machine-level code in a certain format so that the CPU can easily understand it and execute it without error.
Unless both computers run on the same OS, we cannot run code that has been developed on one machine in another when a machine-level language differs by each OS. Many developers and programmers think this is unacceptable. However, Java simplifies things and includes a fix for this.
The JVM, a component of the Java compiler, transforms .java files into.class
files. The key function in the source code is called JVM when we compile Java
It connects to the Java virtual machine as a subsystem. It loads the .class file,
checks for exceptions, and is the cause of a small number of runtime exceptions. After verification, it allows memory for the present variables and methods, gives arrays and variables default values, etc. Additionally, it runs any methods or static variables in the code.
The class loader is carefully designed to handle the task. Hence, the Java runtime is not necessarily expected to be aware of the file and its systems when the classes load.
Bootstrap classloader, Application classloader, and Extension classloader are the three built-in class loaders in JVM. Just a quick note on how these classes function:
Bootstrap Classloader: Java libraries are loaded using the classloader.
Extension Classloader: Load codes into extension directories or any other directory that the system property specifies.
Application classloader: The classloader loads any Java.class.path found code and eventually maps it to the CLASSPATH environment variable.
Memory Areas in JVM
Memory Areas in the JVM Class/Method Area
Method names, class names, and variable names are among the data it stores. It serves as a centralized resource for all the methods and variables in a class. It functions as a memory that holds necessary data to complete the task. It is shared across all the threads of the computer. The execution will determine if it is a fixed or expandable size.
The Heap keeps track of all the data that is kept in its area. It keeps the data in a specific amount that won't be known until the programme has been executed. It is utilized all through the programme. It is utilized for the allocation of dynamic memory. In the heap region, new items are developed.
Language Stack for JVM
When a function is executed, it keeps the current data in blocks, also known as frames or stack-frames maintain local variables and are heavily involved in thread management.
PC Registration System
The address of the instruction that is currently being run is stored there. It is beneficial when there are multiple threads being executed.
It tracks all the details of the local techniques utilized. It has access to system-specific functions that Java does not have access to. This project's main goal is to increase system performance through memory-level communication.
The execution engine will run the bytecode and release the memory allocated to any unreferenced objects. By reading the bytecode in a stream and interpreting it line by line, the interpreter in the execution engine will execute it.
It also features an interesting feature called the JIT compiler (Just In Time Compiler), which creates native executable code for functions so that the JVM doesn't have to interpret them twice if there are repeated function calls. As a result, execution performance is enhanced.
Native Method Interface
Java interacts with programmes written in other languages, such as C/C++, via this interface. The interaction is two-way; the programs themselves or the JVM may call each other. Due to the system-specific application of source code, it has limited portability. It makes it easier for the native code (written in C or C++) to interface with other programs ( written in Java or other languages). Additionally, this is what gives Java platform independence.
Native Method Libraries
It maintains track of any native language libraries that the execution engine could require. They are categorized as a collection of programmes created for a certain architecture. These files are platform-specific.
Let's start our conversation now that we know the JVM's architecture. The result of those JVMs will be the same for the same byte code input, even though this JVM is different for various operating systems.
Platform Independent and Platform Dependent
We may make the byte code intelligible on any platform by using the Java Virtual Machine. Because of this, byte code is referred to as being platform-independent. On the other hand, the Java Virtual Machine is referred to as platform-dependent because it differs for each platform. Because it is not dependent on any one platform, Java is platform-independent. Java is a framework language as a result.
- Programs are translated into framework bytes when they are translated into Java.
- The Java Virtual Machine, which depends on the platform, runs the bytes of code.
- Java works across all platforms.
- The Java Virtual System is required by any machine that wants to run the byte code.
What Role do Compilation and Interpretation Play in Platform In-dependency?
To accomplish platform independence, it combines both compilation and interpretation. When a programme is compiled into Java, it is turned into bytecode, often known as a.class file, rather than machine code. Now, this bytecode is something that distinguishes it from machine code with its features since it can be moved to/portable to another platform, but Java cannot. That bytecode may be executed on a Java Virtual Machine.
Why is Java Not Completely Platform Independent?
The important point in Java is that the JVM depends on the operating system; as a result, if you are using Mac OS X, your JVM will differ from someone using Windows or another operating system. This fact may be confirmed by attempting to download the JVM for your specific computer. When doing so, you will be presented with a selection of JVMs corresponding to various operating systems; you should clearly choose the JVM intended for the operating system you are now using. We may thus conclude that JVM is platform-dependent, which is how Java was able to become 'Platform Independent.
Hope this article was able to give you a thorough understanding about why and how Java is platform independent. If you are looking to enhance your software development skills even further, we recommend you check Simplilearn’s Post Graduate Program in Full Stack Web Development. This course will help you develop some of the relevant skills and make you job-ready in no time.
If you have any questions or queries, feel free to post them below in the comments section. Our team will review and get back to you at the earliest.