Home Java javaTutorial Why is java cross-platform?

Why is java cross-platform?

May 22, 2019 pm 05:51 PM
java jvm

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.

Why is java cross-platform?

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.

Why is java 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles