What is platform independence?

I believe that for many Java developers, when they first came into contact with the Java language, they heard that Java is a cross-platform language, and Java is platform-independent. This is also an important reason why the Java language can rise rapidly and enjoy unlimited glory. . So, what exactly is platform independence? How does Java achieve platform independence? This article will give a brief introduction.

What is platform independence

Platform independence means that the operation of a language on a computer is not restricted by the platform, compile once and execute everywhere (Write Once, Run Anywhere).

In other words, executable binary programs created in Java can run on multiple platforms without modification.

Benefits of platform independence

As a platform-independent language, it is outstanding both in terms of its own development and its friendliness to developers.

Because of its platform independence, Java programs can run on various devices, especially some embedded devices, such as printers, scanners, fax machines, etc. With the advent of the 5G era, more terminals will be connected to the network. I believe that platform-independent Java can also make some contributions.

For Java developers, Java reduces the cost and time of developing and deploying to multiple platforms. Really compile once and run everywhere.

Implementation of platform independence

Support for Java's platform independence, like support for security and network mobility, is distributed throughout the Java architecture. Among them, Java language specification, Class file, Java virtual machine (JVM) and so on play an important role.

Basics of Compilation Principles

When it comes to the Java language specification, Class files, and Java virtual machines, we have to mention how Java actually runs.

We introduced in [Compilation and decompilation of Java code] [1], in the computer world, computers only know 0 and 1, so what is actually executed by the computer is actually a binary file composed of 0 and 1 .

However, C, C++, Java, Python, etc. that we use in daily development are all high-level languages, not binary languages. Therefore, if you want the computer to understand the Java code we wrote, you need to "translate" it into a binary file composed of 0 and 1. This process is called compilation. The tool responsible for this process is called a compiler.

In [In-depth analysis of Java compilation principles][2], we introduced that on the Java platform, if you want to compile Java files into binary files, you need to go through two steps of compilation, front-end compilation and back-end compilation:

![][3]

Front-end compilation mainly refers to the part related to the source language but not related to the target machine. In Java, the compilation we are familiar with javacis front-end compilation. In addition to this, many IDEs we use, such as eclipse, idea, etc., have built-in front-end compilers. The main function is to .javaconvert code into .classcode.

The code mentioned here .classis actually the Class file.

Back-end compilation is mainly to translate the intermediate code into machine language. In Java, this step is performed by the Java virtual machine.

![][4]

Therefore, we said that Java's platform-independent implementation mainly acts on the above stages. As shown below:

![][5]

Let's introduce these three leading actors from back to front: Java virtual machine, Class file, Java language specification

Java virtual machine

The so-called platform independence means that it can be seamlessly connected on multiple platforms. However, for different platforms, hardware and operating systems are definitely not the same.

For different hardware and operating systems, the main difference is that the instructions are different. For example, if a+b is also executed, the binary instruction corresponding to operating system A may be 10001000, while the instruction corresponding to operating system B may be 11101110. Then, if you want to be cross-platform, the most important thing is to be able to generate corresponding binary instructions according to the corresponding hardware and operating system.

And this work is mainly done by our Java virtual machine. Although the Java language is platform-independent, the JVM is platform-dependent, and corresponding JVMs must be installed on different operating systems.

![][6]

The above figure is the guide for downloading JDK from Oracle official website. Different operating systems need to download the corresponding Java virtual machine.

With the Java virtual machine, if you want to perform a+b operation, the virtual machine on the A operating system will translate the instruction into 10001000, and the virtual machine on the B operating system will translate the instruction into 11101110.

![][7] ps: The contents of the Class file in the figure are mock contents

Therefore, the reason why Java can be cross-platform is because the Java virtual machine acts as a bridge. It acts as a buffer between the runtime Java program and the underlying hardware and operating system.

bytecode

Virtual machines on various platforms use a unified program storage format-bytecode (ByteCode) is another cornerstone of platform independence. The Java virtual machine only interacts with Class files composed of bytecodes.

We say that the Java language can Write Once, Run Anywhere. Write here actually refers to the process of generating Class files.

Because a Java Class file can be created on any platform, it can also be loaded and executed by a Java virtual machine on any platform, so Java is platform-independent.

Java Language Specification

With a unified Class file and a Java virtual machine that can translate Class files into corresponding binary files on different platforms, can Java be completely cross-platform?

In fact, it is not. The Java language has also made some efforts in cross-platform, and these efforts are defined in the Java language specification.

For example, the range and behavior of basic data types in Java are defined by themselves. In C/C++, the basic data type is determined by its occupation width, and the occupation width is determined by the platform. Therefore, on different platforms, the compilation results of the same C++ program will behave differently.

To give a simple example, for the int type, in Java, int occupies 4 bytes, which is fixed.

But it is not fixed in C++. On a 16-bit computer, the length of the int type may be two bytes; on a 32-bit computer, it may be 4 bytes; when a 64-bit computer becomes popular, the length of the int type may reach 8 bytes. (Everything mentioned here is possible!)

![][8]

By ensuring the consistency of basic data types on all platforms, the Java language provides strong support for platform independence.

summary

Support for Java's platform independence is distributed throughout the Java architecture. Among them, the Java language specification, Class file, and Java virtual machine play an important role.

  • Java Language Specification
    • By specifying the value range and behavior of basic data types in the Java language
  • Class file
    • All Java files should be compiled into a unified Class file
  • Java virtual machine
    • Convert the Class file into the binary file of the corresponding platform through the Java virtual machine, etc.

The platform independence of Java is based on the platform dependence of the Java virtual machine, because the Java virtual machine shields the differences of the underlying operating system and hardware.

Language independence

In fact, the independence of Java is not only reflected in the platform independence, but to expand outward, Java also has language independence.

We mentioned it earlier. The JVM does not actually interact with Java files, but with Class files. That is to say, when the JVM is running, it does not depend on the Java language.

Today, commercial organizations and open source organizations have developed a large number of languages ​​that can run on the JVM outside of the Java language, such as Groovy, Scala, Jython, etc. The reason why it can be supported is that these languages ​​can also be compiled into bytecodes (Class files). The virtual machine does not care which language the bytecode is compiled from.

Guess you like

Origin blog.csdn.net/zy_dreamer/article/details/132364931