Week 1

Environment setup

How Java works

Types and Variables

Arrays

Basic IO

Control Flow

Packages

OOP in Java

Static Members

Practice

Assignment

Back end Track

Bytecode

Unlike Javascript, Java is a compiled programming language, that means source code has to be compiled into bytecode before it can be executed by the Java Virtual Machine.

Bytecode is the bridge between the human-readable source code and the machine-readable binary code that the CPU understands.

Bytecode is the universal language of the JVM. Source code compiled into bytecode is platform independent—the same bytecode will run identically on Windows, Linux, or macOS computers.

<aside> ⚙

The Java compiler (javac) converts source code .java files into .class files through compilation.

</aside>

Let’s assume that we have the following piece of Java source code :

int localValue = 5 + 10;

The bytecode instructions for this piece of code would look something like this:

0: iconst_5      // Push integer 5 onto the stack
1: bipush 10     // Push byte 10 onto the stack
3: iadd          // Add the two integers
4: istore_1      // Store the result in local variable 1

<aside>

  1. Source Code (.java): What you write. (Human-friendly)
  2. Bytecode (.class): What javac produces. (JVM-friendly)
  3. Machine Code: What the JVM translates bytecode into at runtime. (CPU-friendly) </aside>

In the early days, the JVM "interpreted" bytecode line-by-line, which was slow. Modern JVMs use a JIT (Just-In-Time) Compiler. As your program runs, the JVM identifies "hot" sections of bytecode (code that runs frequently) and compiles them directly into lightning-fast machine code on the fly. It allows Java to be faster than interpreted JS but more portable than C++ ( in some cases, it's even faster than languages compiled to machine code because the JVM can optimize and modify bytecode in real time ).

JVM

The Java Virtual Machine (JVM) is the heart of Java—it executes compiled bytecode. It's the main reason Java programs are platform independent. JVM implementations are platform dependent, but Java programs are not.

Class Loader

Class Loader dynamically loads .class files into memory only when they are needed.

Memory Management

  1. Heap

    Heap is the memory where all objects live and it’s accessible by all threads

  2. Stack

    Stack is where local variables and method calls live. Each thread gets its own stack

  3. Metaspace

    Where the JVM stores class metadata (the "blueprints")

The Execution Engine

  1. The Interpreter

    Reads bytecode and executes it line-by-line (Starts fast, runs slow)

  2. The JIT (Just-In-Time) Compiler

    It finds "hot code" (loops or methods called thousands of times) and compiles them into Native Machine Code

  3. The Garbage Collector (GC)

    Automatically reclaims memory. Modern JVMs (like G1 or ZGC) can clean up memory with almost zero pause time.

<aside> ⚠️

The JVM is polyglot—it doesn't care about the Java language, only bytecode. This means it can run Kotlin, Scala, and Groovy programs as well.

</aside>

JRE

Java Runtime Environment (JRE) consists of JVM + standard libraries (math, strings, networking etc.). It’s everything needed to run a Java program.

JRE is usually bundled inside the JDK.


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*

Found a mistake or have a suggestion? Let us know in the feedback form.