Understanding Java: Compiled or Interpreted, Variables, Garbage Collection and Memory Management

Omkar Bhavare
3 min readApr 26, 2024

--

What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle Corporation).Java is known for its platform independence, which means that programs written in Java can run on any device that has a Java Virtual Machine (JVM) installed, without the need for recompilation.

What makes Java so special?
For Beginners , it’s a compiled language, but not in the traditional sense.Instead of being directly compiled into machine code, Java code is first compiled into an intermediate form called bytecode. This bytecode is then executed by the Java Virtual Machine (JVM), which translates it into the machine-specific instructions that can be run on the computer’s processor. This process is called Just-In-Time (JIT) compilation.This unique approach to compilation allows Java programs to be platform-independent, meaning they can run on any device that has a JVM installed, without the need for recompilation.

Java JIT compilation

Variables in Java

In Java, variables are used to store values of a specific data type, such as integers, floating-point numbers, characters, or boolean values. Variables are declared using a data type and a name, for example: int age = 25; or String name = "John";. Here int → Data Type , age → Variable and 25 is value.

Memory Allocation: Stack and Heap:

  • Stack: The stack is used for storing primitive data types (such as int, char, boolean) and references to objects. It operates in a Last-In-First-Out (LIFO) manner and is limited in size. When a method is called, a new stack frame is created to store local variables and method parameters. When the method completes execution, its stack frame is popped off the stack.
  • Heap: The heap is a larger, more flexible area of memory used for allocating objects. Objects created in Java are stored in the heap, regardless of where they are declared in the code. Unlike the stack, the heap memory is not automatically deallocated when a method completes. Instead, it is managed by the Java Virtual Machine’s garbage collector.

Role of References: In Java, objects are accessed through references. When an object is created, memory is allocated for it in the heap, and a reference to that memory location is returned. Variables of object types store these references rather than the actual object itself. This allows multiple variables to refer to the same object and enables passing objects between methods efficiently.

Garbage Collection:

Java employs automatic memory management through a process known as garbage collection. The garbage collector periodically identifies and removes objects from memory that are no longer reachable or referenced by any part of the program. This helps free up memory resources and prevents memory leaks.The garbage collector uses a Mark-And-Sweep Algorithm to mark all unreachable objects as garbage collection.

Click on the Topic for a Deeper Understanding

👉 Java Just-In-Time Compilation
👉 Memory Allocation: Stack and Heap
👉 Java Architecture

--

--

Omkar Bhavare

Passionate developer, blogger, and problem solver. Crafting code, sharing insights, and building innovative projects. 🚀