JVM (Java Virtual Machine) – How Java Actually Runs Your Code Today I revised the internal workflow of the JVM and understood how .class files are loaded, verified, executed, and managed in memory. 1. Class Loader Subsystem Responsible for bringing .class files into memory. It has three main loaders: -->Bootstrap ClassLoader – loads core Java classes -->Extension ClassLoader – loads extension libraries -->Application ClassLoader – loads user-defined classes After loading, JVM performs: -->Verification – checks bytecode safety -->Preparation – allocates memory for static fields -->Resolution – replaces symbolic references -->Initialization – runs static blocks and static variables 2. Runtime Data Areas JVM internally divides memory into: -->Method Area: class code, static data, metadata -->Heap: objects + instance variables -->Stack: method calls + local variables (per thread) -->PC Registers: next instruction of each thread -->Native Method Stack: C/C++ code used via JNI 3. Execution Engine -->Handles actual execution: -->Interpreter: line-by-line execution -->JIT Compiler: converts hotspots into machine code for speed -->Garbage Collector: frees unused memory 4. Native Method Interface (JNI) Allows Java to interact with native libraries (C/C++). Understanding the JVM is essential to writing efficient, memory-safe Java code. Thanks Prasoon Bidua sir for making JVM fundamentals easy to grasp. #Java #JVM #CoreJava #MemoryManagement #LearningInPublic
JVM Workflow: Class Loading, Verification, Execution, and Memory Management
More Relevant Posts
-
☕ JAVA ARCHITECTURE — 2 Minute Explanation Crack it in interview This diagram shows how a Java program runs from source code to hardware using the Java Virtual Machine (JVM) 🧠 🧑💻 Step 1 — Java Source Code At the top left, we write code in a .java file 📄 This is human-readable, but the machine cannot understand it directly. ⚙️ Step 2 — Compilation The javac compiler converts the .java file into bytecode (.class file`) 🔄 This bytecode is platform-independent, meaning it can run on any system that has a JVM 🌍 This is where Java achieves: > ✨ Write Once, Run Anywhere ❤️ Step 3 — JVM (Heart of Architecture) The bytecode is executed inside the JVM, not directly by the operating system. The JVM has three main components: 📦 1️⃣ Class Loader It loads .class files into memory and performs: ✔️ Loading ✔️ Linking ✔️ Initialization It also verifies code for security 🔐 🧠 2️⃣ Runtime Memory Areas JVM divides memory into sections: 📘 Method Area → class info & static data 🟢 Heap → objects 🔵 Stack → method calls & local variables 📍 PC Register → current instruction 🧩 Native Method Stack → native code This structured memory makes Java stable and secure 🛡️ 🚀 3️⃣ Execution Engine This runs the program: ▶️ Interpreter executes bytecode line by line ⚡ JIT Compiler converts frequently used code into machine code for speed 🧹 Garbage Collector automatically removes unused objects This is why Java is both fast ⚡ and memory safe 🧠 🔌 Step 4 — JNI & Native Libraries If Java needs OS-level features, it uses JNI to interact with native libraries written in C/C++ 🧩 🔄 Final Flow .java → Compiler → Bytecode → JVM → OS → Hardware 🎯 Closing Line > “Java architecture uses bytecode and the JVM to provide platform independence 🌍, structured memory management 🧠, runtime optimization ⚡ through JIT, and automatic garbage collection 🧹, ensuring secure 🔐 and high-performance 🚀 execution.” #java #javaarchitecture #jvm #jre #jdk
To view or add a comment, sign in
-
-
Stop writing boilerplate. Let the compiler do the heavy lifting. In modern Java, you can replace a verbose data class with a single line: public record Car(String name) {} Behind the scenes, Java automatically generates: ✅ Private final fields (Immutability by default). ✅ Canonical constructor (State initialization). ✅ Accessor methods (e.g., car.name()). ✅ equals() and hashCode() (Value-based equality). ✅ toString() (Readable output). // What the JVM actually sees: public final class Car extends java.lang.Record { privatefinal String name; public Car(String name) { this.name = name; } public String name() { returnthis.name; } @Overridepublic boolean equals(Object o) { ... } @Overridepublic int hashCode() { ... } @Overridepublic String toString() { ... } } The Result? Cleaner code, fewer bugs, and zero "boilerplate fatigue." Are you still using traditional POJOs, or have you switched to Records? 👇 #Java #CleanCode #SoftwareEngineering #ProgrammingTips #ModernJava
To view or add a comment, sign in
-
🚀 Today I went through an interesting backend concept — HashMap Collision (Java) ❓ Question Why do collisions happen in HashMap, and how does Java handle them (Java 8)? --- ⚠️ The Problem — What is Collision? HashMap internally stores data in an array of buckets. Whenever a key is inserted, Java calculates: index = hash(key) % array_size Since the array size is limited (16, 32, 64…), different keys can sometimes generate the same index. Example: hash("CAT") = 18 hash("DOG") = 10 array_size = 8 18 % 8 = 2 10 % 8 = 2 Both keys go to index 2 → Collision occurs. --- 🧠 Understanding Hash A hash is simply a numeric value generated from a key using a hash function, helping the system quickly decide where the data should be stored. --- ✅ The Solution — How Java 8 Handles Collisions When multiple keys map to the same bucket: 1️⃣ First entry is stored normally 2️⃣ Next entries are stored using a LinkedList in that bucket 3️⃣ If entries become large (≈ 8), LinkedList converts into a Red-Black Tree for faster search Why this matters: - LinkedList search → O(n) - Tree search → O(log n) So even with many collisions, performance remains efficient. --- 📌 Final Insight HashMap performance depends heavily on hashing + bucket indexing. Collisions are normal, but Java smartly optimizes them using LinkedList → Tree conversion to maintain speed. #BackendLearning #Java #HashMap #SpringBoot #SoftwareEngineering #LearningInPublic #JavaDeveloper #cfbr
To view or add a comment, sign in
-
-
𝐎𝐮𝐭 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐎𝐥𝐝, 𝐈𝐧 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐍𝐞𝐰 Java has evolved, and with it, a simpler, more modern approach to writing immutable data types records. In previous versions of Java, creating simple value objects required a significant amount of boilerplate code. 𝐓𝐡𝐞 𝐎𝐥𝐝 𝐖𝐚𝐲 public class Point { private final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } @Override public boolean equals(Object obj) { ... } @Override public int hashCode() { ... } @Override public String toString() { ... } } 𝐓𝐡𝐞 𝐍𝐞𝐰 𝐖𝐚𝐲 Now, with records, all that boilerplate is handled for you. A record automatically generates A constructor equals(), hashCode(), and toString() methods public record Point(int x, int y) {} When you have simple value objects with immutable data. When you don’t need additional logic like setters, mutable fields, or complex methods. #Java #JavaRecords #Programming #Coding #ImmutableData #BoilerplateCode #CleanCode #Java14 #ModernJava #SoftwareDevelopment #CodeSimplification #ObjectOrientedProgramming #JavaBestPractices #JavaTips #JavaDeveloper #TechTrends #DeveloperLife #JavaSyntax #JavaProgramming #RecordClass #TechInnovation #CodingTips #JavaCommunity
To view or add a comment, sign in
-
📁 File Handling 🌊ByteStreams = flow of data as bytes 👉 Byte streams handle raw bytes 🧱 👉 FileInputStream → read 📥 👉 FileOutputStream → write 📤 👉 read() returns -1 at EOF 🛑EndOfFile 👉 Temp variable avoids skipping bytes 🔁 👉 String ➝ getBytes() → byte[] 🔄 👉 Append mode using true ➕ • read() returns 0–255 or -1, so return type is int • int -1 allows JVM to safely signal EndOfFile 🧠 java.io.File 📦 is the package that imports File Class ⚠️ Why try-catch exists • Files live outside JVM 💽 • JVM can’t guarantee file exists, path is valid, or permission is granted 🚫 • So Java forces you to handle risk using checked exceptions 🛡️ • Byte streams → raw data 🧱 • Character streams → text + encoding 📝 👉 Byte streams move raw bytes between JVM and external systems of any format Byte streams deal with bytes, character streams deal with characters using encoding. GitHub Link: https://lnkd.in/eur5pBx4 🔖Frontlines EduTech (FLM) #Java #FileHandling #Streams #BackendDevelopment #JVM #LearningInPublic #ResourceManagement #AustraliaJobs #SwitzerlandJobs #NewZealandJobs #USJobs #BackendDevelopment #Programming
To view or add a comment, sign in
-
-
𝐓𝐡𝐢𝐬 𝐜𝐨𝐝𝐞 𝐬𝐧𝐢𝐩𝐩𝐞𝐭 𝐢𝐬 𝐞𝐧𝐨𝐮𝐠𝐡 𝐭𝐨 𝐜𝐨𝐧𝐟𝐮𝐬𝐞 𝐬𝐨𝐦𝐞𝐨𝐧𝐞 𝐰𝐢𝐭𝐡 𝐉𝐚𝐯𝐚. 😁 Let me introduce you to the concept of wrapper classes and autoboxing first. 𝐖𝐑𝐀𝐏𝐏𝐄𝐑 𝐂𝐋𝐀𝐒𝐒𝐄𝐒: These are classes used to wrap primitive values so we can treat them like objects. This is essential for the Collection framework (like ArrayList), which only works with objects. 𝐀𝐔𝐓𝐎𝐁𝐎𝐗𝐈𝐍𝐆: The process of converting primitive values to wrapper objects is implicit. We don't need the new keyword. Now let's discuss the code: 𝐂𝐎𝐃𝐄 1: 𝘪𝘯𝘵 𝘢 = 100; 𝘪𝘯𝘵 𝘣 = 100; These are primitive values. For primitives, the == operator compares the actual values. Since both are 100, the output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 2: Integer wrapper classes have an Integer Cache. By default, Java caches all Integer objects in the range of -128 to 127. 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘱 = 100; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘲 = 100; Since 100 is in the cache, Java points both variables to the same object memory location. The output is 𝐭𝐫𝐮𝐞. 𝐂𝐎𝐃𝐄 3: 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘺 = 128; 𝘐𝘯𝘵𝘦𝘨𝘦𝘳 𝘻 = 128; Because 128 is outside the cache range, Java creates two distinct objects in memory. Since == compares memory references for objects, the output is 𝐟𝐚𝐥𝐬𝐞. ~Anuprash Gautam 🍵 𝘒𝘦𝘦𝘱 𝘭𝘦𝘢𝘳𝘯𝘪𝘯𝘨 𝘢𝘯𝘥 𝘣𝘳𝘦𝘸𝘪𝘯𝘨 𝘤𝘰𝘧𝘧𝘦𝘦.😊 #Java #programming #autoboxing #oops
To view or add a comment, sign in
-
-
Everyone talks about how to use the this keyword in Java… but very few explain why we actually need it. Let’s understand this with a simple thought process 👇 🔴 The Problem (without this) Imagine a class where the constructor parameters have the same name as instance variables. Java gets confused: 👉 Am I assigning the value to the variable inside the constructor or to the object itself? Result? The object’s data does not get updated the way you expect. 🟡 What’s Really Going Wrong? Java always gives priority to local variables over instance variables. So without clarity, the object never knows “which variable is mine?” 🟢 The Solution (this keyword) The this keyword clearly tells Java: 👉 “I’m talking about the current object’s variable.” Now the assignment is clear, clean, and correct. 💡 In simple words: this removes confusion between object data and temporary data. Once you understand why this exists, you’ll never forget how to use it. #Java #JavaBasics #OOP #LearningJava #ProgrammingConcepts #CodingJourney
To view or add a comment, sign in
-
-
🚀 JVM Class Loader – Explained Visually Ever wondered how Java loads your classes before execution? This image breaks down the JVM Class Loading mechanism step by step 👇 🔹 1. From Source Code to Bytecode We start with MyClass.java The Java compiler (javac) converts it into bytecode → MyClass.class Bytecode is platform-independent and ready for the JVM 🔹 2. Class Loaders in JVM JVM uses a hierarchical class loading system: 🔸 Bootstrap Class Loader Loads core Java classes (java.lang, java.util, etc.) Comes from rt.jar (or module system in Java 9+) 🔸 Extension Class Loader Loads classes from the extensions directory Optional libraries provided to JVM 🔸 Application Class Loader Loads application-level classes From classpath (-cp, -classpath) 👉 Parent Delegation Model ensures security (Class request goes parent → child, not the other way around) 🔹 3. Runtime Memory Areas Once classes are loaded, they live in JVM memory: 📌 Method Area – Class metadata, bytecode, static variables 📌 Heap – Objects and instances 📌 Stack – Method calls and local variables 🔹 4. Linking Phase Before execution, JVM performs: Verify – Bytecode safety checks Prepare – Allocate memory for static fields Resolve – Convert symbolic references to actual memory references 🔹 5. Initialization & Execution Static blocks execute main() starts Application begins running 🎯 💡 Why this matters? Helps debug ClassNotFoundException & NoClassDefFoundError Crucial for performance tuning, frameworks, and JVM internals A must-know concept for senior Java developers #Java #JVM #ClassLoader #JavaInternals #BackendDevelopment #SoftwareEngineering #InterviewPrep #JavaDeveloper
To view or add a comment, sign in
-
-
🚀 Understanding the Latest Java JVM Architecture (Simplified) Java’s real power doesn’t stop at writing clean code—it comes alive inside the Java Virtual Machine (JVM). This architecture image captures how modern JVM works under the hood. 🔹 From Source to Execution Java source code (.java) is compiled by javac into platform-independent bytecode (.class). This bytecode is what makes Java Write Once, Run Anywhere. 🔹 Class Loader Subsystem Dynamically loads classes at runtime Verifies, links, and initializes classes securely Enables modular and scalable applications 🔹 Runtime Data Areas (Memory Model) Heap – Stores objects (managed by GC) Method Area – Class metadata, static data Java Stack – Method calls & local variables PC Register – Tracks current instruction Native Method Stack – Supports native code 🔹 Execution Engine Interpreter executes bytecode line by line JIT Compiler boosts performance by converting hot code into native machine instructions This is where Java achieves near-native performance 🚀 🔹 Garbage Collector & Memory Management Automatically frees unused memory Modern GCs (G1, ZGC, Shenandoah) focus on low latency and high throughput 🔹 JNI & Native Libraries Allows Java to interact with OS-level and native code when required 💡 Why this matters for developers? Understanding JVM internals helps you: Write high-performance applications Tune memory & GC effectively Debug production issues with confidence 👉 JVM is not just a runtime—it’s the heart of Java’s scalability, security, and performance. #Java #JVM #JavaArchitecture #BackendDevelopment #Performance #GarbageCollection #SeniorJavaDeveloper #SoftwareEngineering
To view or add a comment, sign in
-
-
A small Java mistake that wasted hours — but taught a solid lesson Today I hit a bug that looked impossible at first. Data was present in the DB ✅ API worked the first time ✅ After some calls, response started returning null ❌ Logs, alerts, queries — everything looked correct After checking DB and JPA logic thoroughly, the real issue was… one line of code. if (senderId != receiverId) { // fetch data } Looks harmless, right? But this is where things broke. What went wrong In Java, != compares object references, not values. So when senderId and receiverId are: Long Integer String any wrapper/object type The condition may behave inconsistently depending on JVM object creation and caching. Same value ≠ same reference → condition fails → query not executed → null response. That’s why it worked once and failed later. The fix if (!Objects.equals(senderId, receiverId)) { // fetch data } Compares values Null-safe Predictable behavior Key takeaway Never use == or != for value comparison on objects in Java. This wasn’t: a DB issue a JPA issue a query issue It was a Java fundamentals issue showing up at runtime. Small mistakes like this are easy to miss — and that’s exactly why understanding core language behavior still matters, even with modern frameworks. Every backend developer hits this once. The important part is learning why it happened. #Java #SpringBoot #BackendDevelopment #Debugging #CleanCode #LearningInPublic
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development