Java virtual machine facts for kids
Designer | Sun Microsystems |
---|---|
Bits | 32-bit |
Introduced | 1994 |
Version | 20.0.1 |
Type | Stack and register–register |
Encoding | Variable |
Branching | Compare and branch |
Endianness | Big |
Open | Yes |
Registers | |
General purpose | Per-method operand stack (up to 65535 operands) plus per-method local variables (up to 65535) |
A Java virtual machine (JVM) is a special computer program. It lets your computer run programs written in Java. It can also run programs made with other languages that are changed into Java bytecode.
Think of the JVM as a translator. It takes the Java code and makes it understandable for your computer. This means Java programs can run on many different types of computers. This is because the JVM handles the differences in the computer's parts.
The rules for how a JVM should work are written down in a detailed plan. This plan makes sure that Java programs work the same way everywhere. This means people who create Java programs don't need to worry about what kind of computer you have.
The main version of the JVM is called OpenJDK. It is free for everyone to use. It includes a special part called HotSpot. This part helps Java programs run very fast. Companies like Oracle also offer their own versions of the JVM.
How the JVM Works: The Blueprint
The Java virtual machine is like a blueprint for a computer. It's not a real physical computer, but a set of rules. These rules describe how Java programs should run. The JVM is a key part of the Java runtime environment.
The blueprint doesn't say exactly how the JVM should clean up old data. This process is called garbage collection. It also doesn't say how to make Java instructions run faster. This gives people who build JVMs freedom to make them work best. Any Java program needs a real JVM to run.
Oracle has a JVM called HotSpot. They also have another one called JRockit. Oracle owns the Java name. They can check if other JVMs follow their rules.
Class Loader: Building Blocks of Code
In the JVM, programs are made of parts called "classes." A class is like a building block of code. The class loader is a special part of the JVM. It finds and loads these class building blocks. It must be able to read files that follow the Java class file format.
The class loader does three main things in a specific order:
- Loading: It finds and brings in the program's data.
- Linking: This step has three parts:
- Verification: It checks if the program's data is correct and safe.
- Preparation: It sets aside memory for the program's parts. It also gives them default values.
- Resolution: It changes special names in the program into direct links.
- Initialization: It runs the Java code that sets up the program's parts. This gives them their correct starting values.
There are different types of class loaders. Every JVM needs a main one to load important parts. It also needs others to load your program's parts. The rules don't say how a class loader should find these parts.
Inside the JVM: How it Stores Data
The JVM works with specific types of data. These include numbers (like integers and decimals) and references to objects. Older JVMs mostly worked with 32-bit numbers. Larger numbers, like 64-bit ones, used two storage spots.
Smaller types of data, like true/false values or single letters, are treated as 32-bit numbers. This makes them easier for the JVM to handle. Newer JVMs can work with 64-bit numbers directly.
Using a 64-bit JVM can let programs use more memory. This is good for very large programs. However, 64-bit JVMs can sometimes be a bit slower than 32-bit ones.
The JVM has a special memory area called the "heap." This is where it stores objects and lists of data. The JVM also has a "method area." This is where it keeps the program's code and other important information.
Each part of a running program (called a "thread") has its own memory space. This is called a "Java Virtual Machine stack." When a method (a small task in the program) is called, a new "frame" is made. This frame holds information for that task. It disappears when the task is done.
Each frame has an "operand stack" and "local variables." The operand stack helps with calculations. Local variables are like temporary storage spots. They also help pass information to methods. This means the JVM uses both stack and register methods to work.
Bytecode Instructions: The JVM's Language
The JVM understands its own special language called "bytecode." These are instructions for different tasks.
These tasks include:
- Loading and saving data
- Doing math
- Changing data types
- Creating and changing objects
- Managing data on the operand stack
- Controlling where the program goes (like jumping to different parts)
- Calling and returning from methods
- Handling errors (exceptions)
- Managing tasks that run at the same time
The goal is for Java programs to work the same everywhere. Each computer system needs its own JVM. These JVMs read the bytecode in the same way. But how they actually run it can be different.
These instructions work with general types of data. They don't use the specific data types of any one computer.
JVM Languages: More Than Just Java
A JVM language is any language that can be turned into a valid class file. This class file can then run on the Java Virtual Machine. A class file contains JVM instructions (Java bytecode) and other important details. This file format works on any computer.
Many languages can run on the JVM. Some are older languages that were changed to work with the JVM. Examples include JRuby (for Ruby) and Jython (for Python).
New languages have also been made just for the JVM. Some popular ones are Clojure, Groovy, Scala, and Kotlin. A cool thing about JVM languages is that they can work together. For example, a program written in Scala can use parts written in Java.
Bytecode Verifier: Keeping Things Safe
A core idea of Java is safety. Java programs are designed not to crash your computer. They also can't mess with other programs. Java tries to stop common errors that can cause problems. For example, it stops programs from trying to use memory they shouldn't.
Several features in Java help make it safe. These include how classes are set up and how memory is managed. The "verifier" is another important safety feature.
The JVM checks all bytecode before it runs. This verification mainly does three things:
- It makes sure that jumps in the code always go to valid places.
- It checks that data is always set up correctly. It also checks that references to data are safe.
- It strictly controls access to private parts of the code.
The first two checks happen when a class is loaded. The third check happens when one part of the program tries to use another part.
The verifier only allows certain bytecode patterns. For example, a jump instruction can only go to another instruction within the same method. This helps make sure programs are safe and run smoothly.
Secure Execution: Running Remote Code Safely
The JVM allows very careful control over what a program can do. This is designed to let you run code from the internet safely. This model was used by Java applets.
Once the bytecode is checked, the code runs in a restricted area. This area is called a "sandbox." The sandbox protects your computer from bad or broken code. Sometimes, program creators can get a special digital signature. This lets their applets ask you for permission to do more things. For example, they might ask to save a file on your computer.
Bytecode Interpreter and JIT Compiler: Making Code Fast
Each type of computer needs its own Java bytecode interpreter. Once a computer has one, it can run any Java bytecode program. This means the same program can run on many different computers.
When an interpreter runs Java bytecode, it's usually slower than code made for that specific computer. To fix this, JVMs use just-in-time (JIT) compilers. A JIT compiler translates Java bytecode into the computer's native language while the program is running.
The parts of the program that are translated can then run much faster. This technique is used for parts of the program that run often. This way, a JIT compiler can make the program run much faster overall.
It's important to know that Java bytecode isn't just for Java programs. Programs written in other languages can also be turned into Java bytecode. Also, Java programs can sometimes be turned directly into a computer's native language.
Java bytecode is meant to work on any computer and be secure. Some JVMs only use a JIT compiler and don't have an interpreter.
JVM in Web Browsers: A Changing Role
When Java first came out, the JVM was used to make cool web applications. These were called Java applets. They ran inside your web browser.
However, most web browsers today don't come with a Java plugin. They also don't let you easily add one. The Java browser plugin was removed from Java in 2017.
The Java browser plugin let the JVM run Java applets inside web pages. If you had the plugin, the applet could draw things in a box on the page. Since the plugin included a JVM, any language that could run on the JVM could be used for applets. Applets could access things like your microphone or 3D graphics. But they couldn't change the rest of the web page.
By 2015, the use of Java applets on websites had become very small.
JavaScript JVMs and Transpilation
Since 2016, a tool called JavaPoly lets people use Java libraries directly from JavaScript. This means websites can use Java code even if you don't have Java installed.
Another way to make Java code work on the web is "transpilation." This means changing the Java code or bytecode into JavaScript. This is useful because mobile devices often don't support plugins.
Compiling JVM bytecode to JavaScript is a good idea because bytecode is universal. Some tools that do this include TeaVM and Dragome Web SDK. There are also tools that change specific JVM languages into JavaScript. Examples include the Java-to-JavaScript transpiler in Google Web Toolkit, Clojurescript, and Scala.js.
See also
In Spanish: Máquina virtual Java para niños
- Common Language Runtime
- List of Java virtual machines
- List of JVM languages
- Comparison of Java virtual machines
- Comparison of application virtualization software
- Automated exception handling
- Java performance
- Java processor
- K virtual machine (KVM)