kids encyclopedia robot

Java virtual machine facts for kids

Kids Encyclopedia Facts
Quick facts for kids
Java virtual machine
Designer Sun Microsystems
Bits 32-bit
Introduced 1994
Version Java SE 24 Edit this on Wikidata
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)
JvmSpec7
This picture shows how a Java Virtual Machine (JVM) works, based on the Java SE 7 Edition.

A Java Virtual Machine (JVM) is like a special computer program that lets your computer run programs made with Java. It can also run programs written in other languages that have been changed into a special code called Java bytecode.

Think of the JVM as a translator. When a Java program is written, it's not directly understood by your computer's hardware. Instead, it's turned into bytecode. The JVM then takes this bytecode and translates it into instructions your computer can understand and run. This means a Java program can run on many different types of computers without needing to be rewritten for each one.

The way a JVM should work is described in a detailed set of rules called a "specification." These rules make sure that Java programs work the same way on different JVMs, no matter who made them. This helps programmers create apps that run smoothly everywhere.

The most common JVM is called HotSpot. It's developed by the OpenJDK project and is open source, meaning its code is available for anyone to see and use.

What is the JVM Specification?

The Java Virtual Machine is like an imaginary computer defined by a set of rules. It's a key part of the Java runtime environment, which is what you need to run Java programs. The rules for the JVM don't say exactly how it should clean up old memory (called garbage collection) or how it should make the code run faster. This gives different companies the freedom to build their JVMs in the best way they know how.

Since 2004, changes to the JVM rules have been managed by a group called the Java Community Process. The main goal of these rules is to make sure that different JVMs can all run Java programs in the same way. This means a Java program will work correctly whether it's running on a computer with a JVM from Oracle or another company.

How JVM Handles Memory Cleanup (Garbage Collectors)

When programs run, they use up memory. Sometimes, they use memory for things they no longer need. A "garbage collector" is like a cleanup crew inside the JVM. Its job is to find and remove this unused memory, freeing it up for new things. This helps programs run smoothly and prevents them from running out of memory.

Over the years, different versions of Java have used different types of garbage collectors. Some are designed for speed, others for using less memory, and some for handling many tasks at once. For example, the G1 garbage collector became the standard for newer Java versions, starting around 2014.

How JVM Loads Programs (Class Loader)

In Java, programs are organized into "classes." A class loader is a part of the JVM that knows how to find and load these classes. It makes sure that the program's pieces are brought into the JVM correctly.

The class loader does three main things in a specific order:

  • Loading: It finds the program's parts (binary data) and brings them into the JVM.
  • Linking: It checks if the loaded parts are correct and prepares them.
    • Verification: It makes sure the code is safe and follows all the rules.
    • Preparation: It sets aside memory for the program's main variables.
    • Resolution: It connects different parts of the program so they can work together.
  • Initialization: It runs the Java code that sets up the program's variables with their starting values.

There are different types of class loaders, but every JVM must have a "bootstrap class loader" to load important, trusted parts of Java.

How JVM is Built (Virtual Machine Architecture)

The JVM works with different kinds of data, like whole numbers (integers) or numbers with decimals (floating-point numbers). Older JVMs were mostly designed for 32-bit computers. This meant that larger numbers, like 64-bit numbers, would take up two spots in memory.

However, newer JVMs, like the OpenJDK HotSpot JVM, can work with 64-bit computers directly. This is a big advantage because 64-bit systems can handle much more memory. This is important for very large programs that need a lot of space to run. While 64-bit JVMs can use more memory, they might sometimes be a little slower than 32-bit JVMs for certain tasks.

The JVM has a special area called the "heap" where it stores objects and arrays that your program uses. It also has a "method area" for storing the program's code and other important information. Each part of a running program (called a "thread") also has its own "stack." When a method (a block of code) is called, a new section is added to the stack, and it's removed when the method finishes.

This stack helps the JVM keep track of what's happening in the program. It has an "operand stack" for doing calculations and a place for "local variables" which are like temporary storage spots. This design makes the JVM very flexible.

JVM Instructions (Bytecode)

The JVM understands a special set of instructions called "bytecode." These instructions are like a universal language for Java programs. They tell the JVM what to do, no matter what kind of computer it's running on.

Here are some types of tasks these instructions handle:

  • Moving data around (loading and storing)
  • Doing math (arithmetic)
  • Changing data from one type to another
  • Creating and managing objects
  • Managing the operand stack (adding and removing data)
  • Controlling the flow of the program (like jumping to different parts)
  • Calling and returning from methods
  • Handling errors (exceptions)
  • Managing multiple tasks at once

The goal is that a Java program, once turned into bytecode, can run on any computer that has a JVM. Even though the JVM might be built differently for Windows, macOS, or Linux, it will always understand and run the same bytecode in the same way.

Languages That Run on JVM

A JVM language is any programming language that can be turned into Java bytecode. This means that many languages, not just Java, can run on the Java Virtual Machine.

Some well-known languages that were created to run on the JVM include Clojure, Groovy, Scala, and Kotlin. There are also older languages like Ruby and Python that have versions (JRuby and Jython) that can run on the JVM.

A cool thing about JVM languages is that they can often work together. For example, a program written in Java can use parts (libraries) written in Scala, and vice versa. This makes it easier for programmers to use the best tools for their projects.

How JVM Keeps You Safe (Bytecode Verifier)

One of Java's main ideas is safety. It's designed so that a program can't crash your computer or mess with other programs. It also protects important parts of the system from being changed by untrusted code. Common programming mistakes, like trying to access memory that doesn't exist, are also prevented.

The JVM has a "bytecode verifier" that checks all the bytecode before it runs. This is like a security guard that makes sure everything is in order.

The verifier mainly checks three things:

  • That the program always jumps to valid places in the code.
  • That all data is properly set up and used correctly.
  • That private parts of the code are only accessed by authorized parts of the program.

These checks happen when a program is loaded. This strong checking means that even if a program is designed to run on a "stack machine" (a type of computer design), it can still run very fast on modern "register-based" computers because the JVM can optimize it.

Running Code Safely from the Internet

The JVM's design allows for very strict control over what a program can do. This is especially useful for running code that comes from the internet, like old Java applets. Once the bytecode is checked and verified, the downloaded code runs in a restricted area called a "sandbox." This sandbox is designed to protect your computer from bad or harmful code.

Publishers can also "digitally sign" their applets, which is like putting a trusted stamp on them. If an applet is signed, it can ask your permission to do more things, like access your computer's files.

How JVM Makes Programs Fast (Interpreter and Just-in-Time Compiler)

For every different type of computer hardware, a special Java bytecode "interpreter" is needed. An interpreter reads the bytecode instructions one by one and performs the actions. If a computer has a Java bytecode interpreter, it can run any Java program.

However, interpreting code can be slower than running a program that's been directly translated into the computer's own language. To make Java programs run faster, JVMs often use something called a "just-in-time (JIT) compiler."

A JIT compiler works while the program is running. It translates parts of the Java bytecode into the computer's native language. The parts of the program that are used most often get translated first. Once translated, these parts can run much, much faster than if they were just interpreted. This helps speed up the overall program.

It's important to remember that Java programs don't *have* to be run through a JVM. They can be compiled directly into a computer's native language. Also, programs written in other languages can be compiled into Java bytecode and run on the JVM. The main idea behind Java bytecode is to be able to run on many different types of computers and to be secure.

JVM and Web Browsers

When Java first came out, the JVM was promoted as a way to create cool, interactive web applications. These were called Java applets. They would run inside your web browser using a special "plug-in."

However, by 2018, most web browsers no longer supported Java plug-ins. This is because of security concerns and the rise of other web technologies. The Java browser plug-in was officially removed from Java in 2017. So, you won't see Java applets running in modern web browsers anymore.

JavaScript JVMs and Interpreters

Even though Java applets are gone, there are new ways to use Java code on the web. Since 2016, tools like JavaPoly allow websites to use Java libraries directly from JavaScript, even if you don't have Java installed on your computer. This means web developers can use existing Java code in their web pages.

Converting Java to JavaScript

With mobile devices becoming so popular, and web browsers on these devices not supporting plug-ins, there are efforts to convert Java code into JavaScript. This process is called "transpilation."

You can either convert the original Java code or the Java bytecode into JavaScript. Converting bytecode is useful because it works for any language that runs on the JVM. Some tools that do this include TeaVM and Dragome Web SDK.

Other tools convert specific JVM languages directly to JavaScript. For example, there are tools for converting Java (like Google Web Toolkit), Clojure (Clojurescript), Groovy (GrooScript), and Scala (Scala.js) into JavaScript. This allows these languages to be used to create web applications that run directly in the browser.

See also

Kids robot.svg 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)
kids search engine
Java virtual machine Facts for Kids. Kiddle Encyclopedia.