I believe that for many Java developers, when they just touch the Java language, they have heard that Java is a cross-platform language. Java is platform-independent, which is also an important reason for the rapid rise of Java language and unlimited scenery. So what exactly is platform independence? How does Java achieve platform independence? This article will give a brief introduction.
1
What is platform independence
Platform independence is when a language runs on a computer regardless of the platform. It is compiled Once and Run Anywhere.
That is, executable binaries created in Java can run unchanged on multiple platforms.
Platform independence benefits
As a platform-independent language, it stands out both in terms of its own development and friendliness to developers.
Because of its platform independence, Java programs can run on a wide variety of devices, especially embedded devices such as printers, scanners, fax machines, and so on. With the advent of 5G era, there will be more terminals 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. You actually compile it once, run it everywhere.
2
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, the Java language specification, Class files, Java Virtual Machine (JVM) and so on play an important role.
Fundamentals of Compilation
When it comes to Java language specifications, Class files, and Java virtual machines, you have to talk about how Java actually works.
As we explained in Java code compilation and decompilation, in the computer world, a computer only knows zeros and ones, so what is actually being executed by a computer is a binary of zeros and ones.
However, the C, C++, Java, Python, and so on that we use for daily development are high-level languages, not binary languages. So, in order for the computer to understand our Java code, it needs to “translate” it into a binary of zeros and ones. This process is called compilation. The tool responsible for this process is called a compiler.
In the Java platform, to compile a Java file into a binary file, we need to go through two steps: front-end compilation and back-end compilation:
Front-end compilation mainly refers to the part of the source language that is independent of the target. In Java, compilation of javac as we know it is front-end compilation. In addition to this, many of the ides we use, such as Eclipse, IDEA, etc., have a built-in front-end compiler. The main function is to convert.Java code into.class code.
The.class code mentioned here is actually a class file.
Back-end compilation is mainly about translating intermediate code back into machine language. In Java, this step is performed by the Java Virtual machine.
So, when we say that Java’s platform-independent implementation works mainly in the above phases. As shown below:
Let’s take a look back at the three main actors: the Java Virtual Machine, the Class file, and the Java language specification
The Java virtual machine
Platform independence means being able to work seamlessly across multiple platforms. However, the hardware and operating system are certainly different for different platforms.
For different hardware and operating systems, the main difference is the instruction. For example, if a+ B is also executed, the binary instruction corresponding to OS A may be 10001000, and that corresponding to OS B may be 11101110. The most important thing to do cross-platform, then, is to be able to generate binary instructions based on the hardware and operating system.
This is mostly done by our Java virtual machine. While the Java language is platform-independent, the JVM is platform-dependent, with different JVMS installed on different operating systems.
In the figure above, you need to download the Java VIRTUAL machine (JDK) for different operating systems.
With a Java VM, if you want to perform a+ B operation, the VM on OS A will translate the command to 10001000, and the VM on OS B will translate the command to 11101110.
The content in the Class file in the gain diagram is mock
So, Java can be cross-platform because the Java VIRTUAL machine acts as a bridge. It acts as a buffer between the runtime Java program and its underlying hardware and operating system.
The bytecode
VMS on different platforms use a common program storage format — bytecodes are another building block for platform independence. The Java virtual machine interacts only with Class files consisting of its own code.
We say that the Java language can Write Once,Run Anywhere. Write refers to the process of generating a Class file.
Java’s platform independence comes from the fact that Java Class files can be created on any platform and can be loaded and executed by Java virtual machines on any platform.
Java language specification
Now that we have a unified Class file and a Java virtual machine that can translate Class files into their binaries on different platforms, can Java be fully cross-platform?
No, there are some cross-platform efforts that are defined in the Java language specification.
For example, the range and behavior of basic data types in Java are self-defined. In C/C++, the base data type is determined by its space width, which in turn is determined by the platform. As a result, compilation results for the same C++ program will behave differently on different platforms.
To take a simple example, for int, in Java, int takes up 4 bytes, which is fixed.
But in C++ it is not fixed. On a 16-bit computer, an int might be two bytes long; On a 32-bit computer, it might be 4 bytes; When 64-bit computers became popular, ints could be as long as 8 bytes. (It’s all possible!)
The Java language provides strong support for platform independence by ensuring that basic data types are consistent across all platforms.
3
summary
Support for Java’s platform independence is distributed throughout the Java architecture. Among them, Java language specification, Class file, Java virtual machine and so on play an important role.
-
Java language specification
-
By specifying the value range and behavior of basic data types in the Java language
-
The Class file
-
All Java files are compiled into a unified Class file
-
The Java virtual machine
-
Convert Class files into binaries of the corresponding platform using Java virtual machines
The platform independence of Java builds on the platform independence of the Java Virtual machine because the Java Virtual machine hides underlying operating system and hardware differences.
4
Language independence
In fact, Java independence is not only platform independence, but also language independence. More specifically, Java virtual machine has language independence.
We mentioned that earlier. The JVM does not actually interact with Java files, but with Class files, which means that the JVM does not rely on the Java language to run.
Today, commercial and open source organizations have developed a number of languages that run on JVMS outside of the Java language, such as Groovy, Scala, Jython, etc. It is supported because these languages can also be compiled into bytecode. The virtual machine doesn’t care which language the bytecode is compiled from. HelloWorld output from JVM in 9 languages
The resources
Understanding the Java Virtual Machine in Depth (2nd Edition)
Inside the Java Virtual Machine
Java Language Specification — Based on Java SE 8
Java Virtual Machine Specification Version 8
Forwarding to moments is the biggest support for me.