All Guides01-java-foundations
Core Java Fundamentals: JVM, Compilation, Memory, and Types
Understand JVM runtime structure, compilation, memory regions, garbage collection, and Java primitive vs reference types.
backend-engineerjavajvmmemorytypes
Core Java Fundamentals
Java applications run on the JVM, which provides portability, memory management, and runtime safety.
JVM, JDK, JRE
- JDK includes the compiler, standard libraries, and tools used to build Java applications.
- JRE includes the runtime environment to execute bytecode.
- JVM executes bytecode and isolates the application from platform-specific details.
Compilation Process
javaccompiles.javasource files into.classbytecode files.- Bytecode is portable until a JVM implementation loads it.
- The compiler performs syntax checking, type checking, and bytecode generation.
Bytecode
- Bytecode is the intermediate representation executed by the JVM.
- It is stack-based and independent of processor architecture.
- The JVM verifies bytecode before execution for security and correctness.
JVM Memory Regions
- The JVM divides memory into logical regions such as Heap, Stack, and Metaspace.
- Heap stores objects and arrays, is shared across threads, and is managed by GC.
- Stack stores local variables, method frames, and references; it is thread-local and short-lived.
- Metaspace stores class metadata and native memory required by class loaders.
Primitive vs Reference Types
- Primitive types include
byte,short,int,long,float,double,char, andboolean. - Reference types include objects, arrays, and interfaces.
- Use primitives for performance and avoid unnecessary boxing in hot paths.
- Wrapper classes like
Integer,Long, andBooleanprovide object semantics but can introduce allocations.