Java JVM and bytecode One design goal of Java is
portability, which means that programs written for the Java platform must run similarly on any combination of hardware and operating system with adequate run time support. This is achieved by compiling the Java language code to an intermediate representation called
JVM bytecode, instead of directly to architecture-specific
machine code. JVM bytecode instructions are analogous to machine code, but they are intended to be executed by a
virtual machine (VM) written specifically for the host hardware.
End-users commonly use a
Java Runtime Environment (JRE) installed on their device for standalone Java applications or a web browser for
Java applets. Standard libraries provide a generic way to access host-specific features such as graphics,
threading, and
networking. The use of universal bytecode makes porting simple. However, the overhead of
interpreting bytecode into machine instructions made interpreted programs almost always run more slowly than native
executables.
Just-in-time (JIT) compilers that compile byte-codes to machine code during runtime were introduced from an early stage. Java's Hotspot compiler is actually two compilers in one, with
GraalVM (included in e.g. Java 11, but removed as of Java 16) allowing
tiered compilation. Java itself is platform-independent and is adapted to the particular platform it is to run on by a
Java virtual machine (JVM), which translates the
JVM bytecode into the platform's machine language.
Performance Programs written in Java have a reputation for being slower and requiring more memory than those written in
C++. However, Java programs' execution speed improved significantly with the introduction of
just-in-time compilation in 1997/1998 for
Java 1.1, the addition of language features supporting better code analysis (such as inner classes, the StringBuilder class, optional assertions, etc.), and optimizations in the Java virtual machine, such as
HotSpot becoming Sun's default JVM in 2000. With Java 1.5, the performance was improved with the addition of the package, including
lock-free implementations of the
ConcurrentMaps and other multi-core collections, and it was improved further with Java 1.6.
Non-JVM Some platforms offer direct hardware support for Java; there are micro controllers that can run JVM bytecode in hardware instead of a software Java virtual machine, and some
ARM-based processors could have hardware support for executing JVM bytecode through their
Jazelle option, though support has mostly been dropped in current implementations of ARM.
Automatic memory management Java uses an
automatic garbage collector to manage memory in the
object lifecycle. The programmer determines when objects are created, and the Java runtime is responsible for recovering the memory once objects are no longer in use. Once no references to an object remain, the
unreachable memory becomes eligible to be freed automatically by the garbage collector. Something similar to a
memory leak may still occur if a programmer's code holds a reference to an object that is no longer needed, typically when objects that are no longer needed are stored in containers that are still in use. If methods for a non-existent object are called, a
null pointer exception is thrown. One of the ideas behind Java's automatic memory management model is that programmers can be spared the burden of having to perform manual memory management. In some languages, memory for the creation of objects is implicitly allocated on the
stack or explicitly allocated and deallocated from the
heap. In the latter case, the responsibility of managing memory resides with the programmer. If the program does not deallocate an object, a
memory leak occurs. If the program attempts to access or deallocate memory that has already been deallocated, the result is undefined and difficult to predict, and the program is likely to become unstable or crash. This can be partially remedied by the use of
smart pointers, but these add overhead and complexity. Garbage collection does not prevent
logical memory leaks, i.e. those where the memory is still referenced but never used. Garbage collection may happen at any time. Ideally, it will occur when a program is idle. It is guaranteed to be triggered if there is insufficient free memory on the heap to allocate a new object; this can cause a program to stall momentarily. Explicit memory management is not possible in Java, however it is possible to make the JVM do garbage collection manually. Java does not support C/C++ style
pointer arithmetic, where object addresses can be arithmetically manipulated (e.g. by adding or subtracting an offset). This allows the garbage collector to relocate referenced objects and ensures type safety and security. As in C++ and some other object-oriented languages, variables of Java's
primitive data types are either stored directly in fields (for objects) or on the
stack (for methods) rather than on the heap, as is commonly true for non-primitive data types (but see
escape analysis). This was a conscious decision by Java's designers for performance reasons. Java contains multiple types of garbage collectors. Since Java 9, HotSpot uses the
Garbage First Garbage Collector (G1GC) as the default. However, there are also several other garbage collectors that can be used to manage the heap, such as the Z Garbage Collector (ZGC) introduced in Java 11, and Shenandoah GC, introduced in Java 12 but unavailable in Oracle-produced OpenJDK builds. Shenandoah is instead available in third-party builds of OpenJDK, such as
Eclipse Temurin. For most applications in Java, G1GC is sufficient. In prior versions of Java, such as Java 8, the Parallel Garbage Collector was used as the default garbage collector. Having solved the memory management problem does not relieve the programmer of the burden of handling properly other kinds of resources, like network or database connections, file handles, etc., especially in the presence of exceptions. == Syntax ==