Skip to main content

Command Palette

Search for a command to run...

Overview: From .java → Running Program

Published
3 min read
Overview: From .java → Running Program
I

A highly motivated and experienced full-stack developer with a proven track record of developing and deploying web applications. Skilled in a range of programming languages and frameworks, as well as database technologies. Comfortable working in a fast-paced environment and able to adapt to new technologies quickly. A team player who is also able to work independently when required.

YourCode.java 
     ↓ (javac compiler)
YourCode.class (bytecode)
     ↓ (JVM - Java Virtual Machine)
Runs on any OS (Windows, Linux, Mac, etc.)

This is the magic of Java: Write Once, Run Anywhere (WORA).

Step-by-Step: What Happens Behind the Scenes

1. You write OOP code (classes, objects, inheritance, etc.)

Example:

public class Dog extends Animal {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    public void bark() {
        System.out.println(name + " says Woof!");
    }

    public static void main(String[] args) {
        Dog d = new Dog("Buddy");
        d.bark();
    }
}

2. Compilation with javac (Java Compiler)

When you run:

javac Dog.java

Here’s what happens internally:

PhaseWhat Happens
1. Lexical AnalysisCode → tokens (like public, class, Dog, {, etc.)
2. ParsingTokens → Abstract Syntax Tree (AST) — checks syntax
3. Semantic AnalysisChecks types, access rules (e.g., can you access private field?), resolves inheritance, method overriding, etc.
4. Bytecode GenerationConverts the AST into Java Bytecode (.class file) — low-level, platform-independent instructions

The resulting Dog.class contains bytecode — not machine code!

This bytecode understands OOP concepts like:

  • Classes and objects

  • Inheritance (extends)

  • Polymorphism (method overriding)

  • Encapsulation (private/public)

  • this, super, virtual method calls

3. How OOP Features Are Handled in Bytecode

OOP FeatureHow Java Handles It Internally
ObjectsCreated on heap using new → triggers <init> constructor
InheritanceEach class has a superclass pointer. Method calls use virtual method table (vtable)
PolymorphismMethod calls like animal.speak() are virtual calls — JVM looks up actual method at runtime using vtable
Encapsulationprivate fields → bytecode can't access directly (enforced by verifier)
InterfacesImplemented via interface method tables

4. Execution by JVM (java command)

When you run:

java Dog

The JVM (Java Virtual Machine) loads Dog.class and starts executing:

  1. Class Loader

    • Loads .class files

    • Verifies bytecode is safe (no illegal memory access)

    • Links (resolves references to other classes)

  2. Bytecode Interpreter or JIT Compiler

    • Initially: Interprets bytecode line by line

    • After some runs: JIT (Just-In-Time) Compiler converts hot (frequently used) bytecode → native machine code (super fast!)

  3. Runtime Data Areas

    • Heap: All objects live here (new Dog())

    • Stack: Local variables, method calls

    • Method Area: Class info, static variables

    • PC Register: Current instruction pointer

    • Native Method Stack: For native (C/C++) code

  4. Garbage Collector (GC)

    • Automatically frees memory when objects are no longer used

    • Huge benefit of Java OOP — no manual free() or delete

Summary: Behind the Scenes Flow

Dog.java 
  ↓ javac
Dog.class (bytecode with OOP info: vtables, inheritance, etc.)
  ↓ JVM ClassLoader → Verification → Linking
  ↓ JVM Execution Engine (Interpreter + JIT)
  ↓ Runs as native code on your CPU
  ↓ Objects created on heap, GC cleans up

Why This Design?

  • OOP at language level → easy for developers

  • Bytecode + JVM → portable, secure, high-performance (thanks to JIT)

  • Supports advanced OOP cleanly (even reflection, dynamic proxies, etc.)

Fun Fact

You can see the actual bytecode using:

javap -c Dog.class

It shows instructions like:

invokespecial #3 <Dog.<init>>
invokevirtual #4 <Dog.bark>

These are how OOP method calls become real instructions!

6 views

More from this blog

Low Level Design: গভীর থেকে বোঝা এবং আয়ত্ত করা

ভূমিকা: কেন এই Article? তুমি হয়তো programming শিখেছ। Variable, loop, function, data structure - সব জানো। কিন্তু যখন একটা বড় system বানাতে বসো, তখন মনে হয় কোথা থেকে শুরু করব? কীভাবে organize করব? Code লিখতে লিখতে হারিয়ে যাও একটা maze-এ। এই feeling...

Oct 15, 202520 min read71
Low Level Design: গভীর থেকে বোঝা এবং আয়ত্ত করা

Go-তে Interface কীভাবে Code Decouple করে?

একটা HTTP Server দিয়ে পুরো ব্যাপারটা বুঝে নেওয়া যাক আমরা সবাই জানি Go একটা সিম্পল ল্যাঙ্গুয়েজ, কিন্তু interface নিয়ে অনেকেরই confusion থাকে। আজকে আমরা দেখব কীভাবে interface আসলে তোমার code-কে flexible এবং maintainable বানায়। একটা real-world HTT...

Oct 14, 202520 min read5
Go-তে Interface কীভাবে Code Decouple করে?

তোমার Project-এ Coupled Code কীভাবে খুঁজে বের করবে?

Coupled code খোঁজা মানে হচ্ছে তোমার codebase-এ এমন জায়গা খুঁজে বের করা যেখানে একটা অংশ আরেকটার উপর বেশি depend করছে। এটা একটা detective work — তুমি clue খুঁজবে, pattern দেখবে, এবং সমস্যা চিহ্নিত করবে। চলো step by step শিখি কীভাবে এটা করতে হয়। কেন ...

Oct 14, 202510 min read2

Go-তে Object (Struct Instance) তৈরির সম্পূর্ণ গাইড

Go programming শেখার সময় একটা জিনিস খুব তাড়াতাড়ি বুঝতে হয় - কীভাবে object তৈরি করতে হয়। অন্য language যেমন Java বা Python এ class আছে, কিন্তু Go-তে আছে struct। আর struct এর instance বানানোই হলো object তৈরি করা। আজকের এই blog এ আমরা দেখব Go-তে ob...

Oct 13, 202524 min read3
Go-তে Object (Struct Instance) তৈরির সম্পূর্ণ গাইড
I

Imran Hasan

61 posts

Full-stack developer with experience in developing and managing web applications. Skilled in React, Node.js, HTML, CSS, and JavaScript. Experience in managing website hosting and security.