Cross-platform reasons: Java Virtual Machine (JVM) can be cross-platform; JAVA programs are not run directly on the computer (CPU), but on the Java Virtual Machine (JVM). Each system platform They all have their own JVM virtual machine, so the JAVA language can be cross-platform. Java first compiles Java files into binary bytecode class files, and then JVM interprets and executes the class files; because Java runs on JVM, its code can run on JVMs on different platforms without modification.
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
Why can the JAVA language be cross-platform?
The reason why Java can be cross-platform is because the Java Virtual Machine (JVM) can be cross-platform.
Because the code compiled by the Java program is not code that can be directly run by the hardware system, but an "intermediate code" - bytecode. Then different Java virtual machines (JVM) are installed on different hardware platforms, and the JVM "translates" the bytecode into code that can be executed by the corresponding hardware platform. Therefore, for Java programmers, there is no need to consider what the hardware platform is. So Java can be cross-platform.
Because it has a virtual machine (JVM), the JAVA program is not run directly on the computer, but on the java virtual machine (JVM for short). Each system Each platform has its own virtual machine (JVM), so the JAVA language can be cross-platform.
Java code does not run directly on the CPU, but on the Java Virtual Machine (JVM).
Java first compiles the java file into a class file of binary bytecode, and then the jvm interprets and executes the class file.
Because java runs on jvm, its code can run on jvm on different platforms without modification.
Expand knowledge:
The professional explanation of JVM is as follows
Java Virtual Machine (JVM for short) is a specification for computing devices. It is a fictitious computer that is implemented by simulating various computer functions on an actual computer. The Java virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field. The JVM shields the information related to the specific operating system platform, so that the Java program only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on multiple platforms without modification. When the JVM executes bytecode, it actually ultimately interprets the bytecode into machine instructions for execution on the specific platform.
It’s normal to not understand the above explanation. I didn’t want to post a professional explanation at first, but after thinking about it I decided not to. After all, I still want everyone to have a detailed understanding of the JVM. If you don’t understand the professional explanation, I will tell you in plain English:
JVM, which is the Java virtual machine, is a platform, included under the JRE. When you need to execute a Java program, the JVM compiles and executes it for you. The Java source code we write will generate a .class file after compilation, called a bytecode file. The Java virtual machine is responsible for translating bytecode files into machine code for a specific platform and then running it.
And the JVM is divided into many system versions. No matter which platform you write the Java program on, whether it is Windows, Linux, or macOS, you only need to find the JVM and it will be compiled successfully. and execution. In other words, as long as the corresponding JVM is installed on different platforms, the bytecode files can be run and the Java programs we wrote can be run. So this is the reason why Java has good cross-platform and platform-independent features.
During this process, the Java program we wrote did not make any changes. It can run on different platforms only through the "middle layer" of JVM, truly realizing "compile once and run anywhere" the goal of.
JVM is a "bridge", a "middleware", and the key to achieving cross-platform. Java code is first compiled into bytecode files, and then the JVM translates the bytecode files into machine language , thereby achieving the purpose of running Java programs.
Note: The result of compilation is not to generate machine code, but to generate bytecode. The bytecode cannot be run directly and must be translated into machine code by the JVM before it can be run. The bytecode generated by compilation under different platforms is the same, but the machine code translated by the JVM is different.
So, running a Java program must have the support of the JVM, because the compiled result is not machine code and must be translated again by the JVM before it can be executed. Even if you package your Java program into an executable file (such as .exe), you still need JVM support.
Note: Cross-platform is a Java program, not a JVM. JVM is developed using C/C. It is compiled machine code and cannot be cross-platform. Different versions of JVM need to be installed on different platforms.
About JVM execution efficiency
In the first few years after Java was launched, people had different opinions. Interpreting bytecode must be much slower than running machine code at full speed. Is it worth sacrificing performance in exchange for cross-platform advantages?
However, the JVM has an option to translate the most frequently used bytecode into machine code and save it, a process called just-in-time compilation. This approach is so effective that Microsoft's .NET platform also uses virtual machines.
Now's just-in-time compilers are so good that they have even become competitors of traditional compilers. In some cases, they even surpass traditional compilers because the JVM can monitor runtime information. For example, a just-in-time compiler can monitor and optimize frequently used code and can eliminate function calls (i.e., "inline").
However, Java has some additional overhead that C/C does not have, and critical applications are slower. For example, Java uses a platform-independent drawing method, so the GUI program (client program) executes slowly; the virtual machine also takes time to start.
Recommended related video tutorials: Java video tutorial
The above is the detailed content of Why is java cross-platform?. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software