Java- and platform independence

Java and platform independence : This article is taken from GitHub

I believe that many Java developers who, at the time just contact the Java language, heard that Java is a cross-platform language, Java is platform-independent, which is the Java language and can be an important reason for the rapid rise to fame . So, in the end what is platform-independent? Java is how to achieve the independence of the platform? This article will briefly explain.

What is a platform-independent

Platform independence is constrained language is not running on the computer platform, a compile, execute everywhere (Write Once, Run Anywhere).

In other words, with the executable binaries created in Java can run without modification on multiple platforms.

Platform-independent benefits

As a platform-independent language, both in their own development, or the friendliness of the developers are very prominent.

Because of its platform-independent, so Java programs can run on a wide variety of devices, especially some embedded devices, such as printers, scanners, fax machines and so on. With the advent of 5G era, there will be more terminal access network, believe in platform-independent Java can make some contribution.

For Java developers, Java reduces the cost and time to develop and deploy to multiple platforms. True to compile once, run everywhere.

Platform-independent implementation

Support for independence of the Java platform, as support for mobility and network security, is distributed throughout Java architecture. Java language specification in which the actor has an important role, Class files, Java Virtual Machine (JVM) and so on.

Compiler theory foundation

Talked about the Java Language Specification, Class files, Java virtual machine will have to mention how Java in the end is up and running.

We introduced compiled with those things decompiled Java code, in the computer world, the computer only recognize 0 and 1, so that really is actually made up of 0 and 1 of binary computer-executed.

However, C develop our daily use, C ++, Java, Python belong to high-level language, rather than binary language. So, I want to let the computer know we write Java code, he would need to "translate" into a binary 0's and 1's. This process is called compilation. This process is responsible for processing tool called a compiler.

In-depth analysis of the Java compiler theory, we introduced in the Java platform, Java want to file, compiled into a binary file, compiled in two steps, front-end and back-end compiler compiler through:

Compiled mainly refers to the front portion of the source language related to but independent of the target machine. Java, we know javacof is the front-end compiler compiler. In addition to this, we use a lot of IDE, such as eclipse, idea, etc., have built-in front-end compiler. The main function is to .javaconvert the code into .classthe code.

Here referred to .classthe code, in fact, Class files.

The main back-end compiler intermediate code is then translated into machine language. Java, the Java virtual machine this step is performed.

So, we say, Java's platform independence to achieve a major role in the above stage. As shown below:

We explain this from the back three stars: Java Virtual Machine, Class files, Java Language Specification

Java Virtual Machine

The so-called platform-independent, able to do that is to be able to seamlessly across multiple platforms. However, it does not make sense for the platform, hardware, and operating systems are certainly not the same.

For different hardware and operating systems, the most important difference is the instruction. Such is also performed a + b, A corresponding to the operating system commands may be binary 10001000, and B corresponding to the operating system instructions may be 11,101,110. So, you want to do cross-platform, the most important thing is to be in accordance with the corresponding hardware and operating system generates a corresponding binary instruction.

And this work is mainly done by our Java virtual machine. Although the Java language is platform-independent, but the JVM platform really relevant, different operating systems to be installed above the corresponding JVM.

The figure is the official website to download Oracle's JDK guidelines, different operating systems need to download the corresponding Java Virtual Machine.

With Java virtual machine wants to perform the operation a + b, A above the operating system will translate the virtual machine instructions into 10001000, B above the operating system will translate the virtual machine instructions into 11,101,110.

 ps: Class file figure in content mock content

So, why we can do cross-platform Java, because Java virtual machine to act as a bridge. He played the role of a buffer between the Java program and its hardware and operating system at runtime.

Bytecode

各种不同的平台的虚拟机都使用统一的程序存储格式——字节码(ByteCode)是构成平台无关性的另一个基石。Java虚拟机只与由自己码组成的Class文件进行交互。

我们说Java语言可以Write Once ,Run Anywhere。这里的Write其实指的就是生成Class文件的过程。

因为Java Class文件可以在任何平台创建,也可以被任何平台的Java虚拟机装载并执行,所以才有了Java的平台无关性。

Java语言规范

已经有了统一的Class文件,以及可以在不同平台上将Class文件翻译成对应的二进制文件的Java虚拟机,Java就可以彻底实现跨平台了吗?

其实并不是的,Java语言在跨平台方面也是做了一些努力的,这些努力被定义在Java语言规范中。

比如,Java中基本数据类型的值域和行为都是由其自己定义的。而C/C++中,基本数据类型是由它的占位宽度决定的,占位宽度则是由所在平台决定的。所以,在不同的平台中,对于同一个C++程序的编译结果会出现不同的行为。

举一个简单的例子,对于int类型,在Java中,int占4个字节,这是固定的。

但是在C++中却不是固定的了。在16位计算机上,int类型的长度可能为两字节;在32位计算机上,可能为4字节;当64位计算机流行起来后,int类型的长度可能会达到8字节。(这里说的都是可能哦!)

通过保证基本数据类型在所有平台的一致性,Java语言为平台无关性提供强了有力的支持。

小结

对于Java的平台无关性的支持是分布在整个Java体系结构中的。其中扮演者重要的角色的有Java语言规范、Class文件、Java虚拟机等。

  • Java语言规范
    • 通过规定Java语言中基本数据类型的取值范围和行为
  • Class文件
    • 所有Java文件要编译成统一的Class文件
  • Java虚拟机
    • 通过Java虚拟机将Class文件转成对应平台的二进制文件等

Java的平台无关性是建立在Java虚拟机的平台有关性基础之上的,是因为Java虚拟机屏蔽了底层操作系统和硬件的差异。

语言无关性

其实,Java的无关性不仅仅体现在平台无关性上面,向外扩展一下,Java还具有语言无关性。

前面我们提到过。JVM其实并不是和Java文件进行交互的,而是和Class文件,也就是说,其实JVM运行的时候,并不依赖于Java语言。

时至今日,商业机构和开源机构已经在Java语言之外发展出一大批可以在JVM上运行的语言了,如Groovy、Scala、Jython等。之所以可以支持,就是因为这些语言也可以被编译成字节码(Class文锦啊)。而虚拟机并不关心字节码是有哪种语言编译而来的。

参考资料

《深入理解Java虚拟机(第二版)》 《深入Java虚拟机》 《Java语言规范——基于Java SE 8》 《Java虚拟机规范第8版》

Guess you like

Origin www.cnblogs.com/hxb-00/p/11145465.html