<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohd Rehan</title>
    <description>The latest articles on Forem by Mohd Rehan (@bugtobrilliance).</description>
    <link>https://forem.com/bugtobrilliance</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3379956%2F973948fc-f972-4925-9f85-8218b392cf4c.png</url>
      <title>Forem: Mohd Rehan</title>
      <link>https://forem.com/bugtobrilliance</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bugtobrilliance"/>
    <language>en</language>
    <item>
      <title>Multithreading in Java: A Beginner’s Guide to Handling Concurrency</title>
      <dc:creator>Mohd Rehan</dc:creator>
      <pubDate>Sun, 28 Sep 2025 05:24:44 +0000</pubDate>
      <link>https://forem.com/bugtobrilliance/multithreading-in-java-a-beginners-guide-to-handling-concurrency-45h5</link>
      <guid>https://forem.com/bugtobrilliance/multithreading-in-java-a-beginners-guide-to-handling-concurrency-45h5</guid>
      <description>&lt;p&gt;When I first started learning Java backend development, I hit a challenge: handling multiple user requests at the same time. For example, when two users tried to update shared data, my program sometimes returned inconsistent results. That’s when I realized I needed to understand multithreading and concurrency in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Multithreading?
&lt;/h2&gt;

&lt;p&gt;Multithreading means running multiple tasks (threads) simultaneously inside a program. In backend systems, this is crucial for:&lt;/p&gt;

&lt;p&gt;Handling concurrent user requests.&lt;/p&gt;

&lt;p&gt;Improving performance.&lt;/p&gt;

&lt;p&gt;Preventing applications from “freezing” when one task is slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concepts in Multithreading
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Thread&lt;/strong&gt;: A lightweight unit of execution. You can create a thread by extending Thread or implementing Runnable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Runnable:&lt;/strong&gt; A functional interface used to define the task a thread will execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronization&lt;/strong&gt;: A way to control access to shared resources so multiple threads don’t cause inconsistent data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thread Safety:&lt;/strong&gt; Writing code that behaves correctly even when accessed by multiple threads simultaneously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Example: Creating a Thread&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyTask&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Task is running in: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyTask&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;Multithreading helps backend apps handle concurrent tasks efficiently.&lt;/p&gt;

&lt;p&gt;Always use synchronization or tools like ReentrantLock when dealing with shared resources.&lt;/p&gt;

&lt;p&gt;Debugging multithreaded code requires patience—logs, thread dumps, and careful observation are your best friends.&lt;/p&gt;

&lt;p&gt;👉 Have you faced tricky threading issues in your Java projects? How did you solve them? Share your story—I’d love to learn from you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codecraftbyrehan.hashnode.dev/multithreading-in-java-debugging-my-first-concurrency-issue" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;Summary only — explore the full breakdown on Hashnode →&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>javabackend</category>
      <category>multithreading</category>
      <category>concurrency</category>
      <category>threadsynchronization</category>
    </item>
    <item>
      <title>Spring Framework Explained: Concepts, Features &amp; Why It Matters</title>
      <dc:creator>Mohd Rehan</dc:creator>
      <pubDate>Sat, 20 Sep 2025 19:54:36 +0000</pubDate>
      <link>https://forem.com/bugtobrilliance/spring-framework-explained-concepts-features-why-it-matters-4357</link>
      <guid>https://forem.com/bugtobrilliance/spring-framework-explained-concepts-features-why-it-matters-4357</guid>
      <description>&lt;p&gt;When I first started working with Java, managing dependencies and modularizing code felt like juggling too many balls at once. That’s when I discovered the Spring Framework—a powerful yet lightweight framework that simplifies backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Spring Framework?
&lt;/h2&gt;

&lt;p&gt;At its core, Spring is a Java framework for building enterprise applications. It makes code cleaner, modular, and testable through its principles of Dependency Injection (DI) and Inversion of Control (IoC).&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concepts You Should Know
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection (DI) &amp;amp; IoC:&lt;/strong&gt; Instead of your code manually creating objects, Spring injects them for you. This makes applications loosely coupled and easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aspect-Oriented Programming (AOP):&lt;/strong&gt; Logging, security, and transactions can be modularized into reusable aspects instead of scattered across your codebase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modular Architecture:&lt;/strong&gt; Core container, beans, context, and the Spring Expression Language (SpEL) give developers fine-grained control over application structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transaction Management:&lt;/strong&gt; Spring simplifies working with relational databases and integrates smoothly with ORM tools like Hibernate and JPA.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Does Spring Matter?
&lt;/h2&gt;

&lt;p&gt;Spring has become a cornerstone of Java backend development because it:&lt;/p&gt;

&lt;p&gt;Reduces boilerplate code.&lt;/p&gt;

&lt;p&gt;Increases modularity and testability.&lt;/p&gt;

&lt;p&gt;Offers a vast ecosystem for database integration, messaging, and security.&lt;/p&gt;

&lt;p&gt;Provides enterprise-grade features in a lightweight package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Example: Defining a Bean&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, Spring!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/user"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here, Spring takes care of wiring UserService into UserController without manual instantiation.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Mastering the Spring Framework’s core concepts—DI, IoC, AOP, and transaction management—gives you a strong foundation in Java backend development.&lt;/p&gt;

&lt;p&gt;👉 Have you worked with Spring before? What’s your biggest “aha!” moment with it? Share your thoughts in the comments!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codecraftbyrehan.hashnode.dev/comprehensive-overview-of-spring-framework-concepts-features-and-why-its-important" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;This post is a brief summary. For the full deep dive, check out the complete article on Hashnode →&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>springframework</category>
      <category>javadependencyinjection</category>
      <category>ioccontainer</category>
      <category>javabackendframework</category>
    </item>
    <item>
      <title>A Beginner’s Guide to Exception Handling in Java</title>
      <dc:creator>Mohd Rehan</dc:creator>
      <pubDate>Sat, 20 Sep 2025 19:11:21 +0000</pubDate>
      <link>https://forem.com/bugtobrilliance/a-beginners-guide-to-exception-handling-in-java-2e4n</link>
      <guid>https://forem.com/bugtobrilliance/a-beginners-guide-to-exception-handling-in-java-2e4n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Description&lt;/strong&gt;: A beginner Java backend developer shares a real debugging experience with exception handling in Java. Learn how to spot errors, debug effectively, and implement clean solutions in Spring Boot applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction: My First Real Exception Struggle
&lt;/h2&gt;

&lt;p&gt;When I first started building backend applications with Java and Spring Boot, I assumed exceptions were just scary red errors that stopped my program. That illusion broke quickly when my API, which was supposed to return simple user details, &lt;strong&gt;crashed with a &lt;code&gt;NullPointerException&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The worst part? I didn’t even know where to start. My logs looked overwhelming, my endpoint returned a &lt;strong&gt;500 Internal Server Error&lt;/strong&gt;, and I felt stuck.&lt;/p&gt;

&lt;p&gt;This post shares how I debugged the problem step by step, the resources I relied on, and the solution that made my backend more reliable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: The Initial Symptoms
&lt;/h2&gt;

&lt;p&gt;The first sign of trouble appeared when I hit my &lt;code&gt;/users/{id}&lt;/code&gt; endpoint. Instead of JSON, I got this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;NullPointerException&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Cannot&lt;/span&gt; &lt;span class="n"&gt;invoke&lt;/span&gt; &lt;span class="s"&gt;"String.length()"&lt;/span&gt; &lt;span class="n"&gt;because&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;demo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;java&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;example&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;demo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;java&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 &lt;strong&gt;My application wasn’t failing silently—it was exploding loudly.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Debugging with Logs and Tools
&lt;/h2&gt;

&lt;p&gt;To understand the issue, I added a quick &lt;strong&gt;debug log&lt;/strong&gt; in my service method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DEBUG: Input name is -&amp;gt; "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When I re-ran the request, the console printed:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**DEBUG: Input name is -&amp;gt; null**

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 &lt;strong&gt;That’s when I realized the bug wasn’t in toUpperCase(). The issue was that my input parameter itself was null.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Researching Java Exception Handling
&lt;/h2&gt;

&lt;p&gt;Instead of wrapping everything in random &lt;code&gt;try-catch&lt;/code&gt; blocks, I wanted a cleaner, more &lt;strong&gt;backend-friendly approach&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I searched &lt;em&gt;“Spring Boot exception handling best practices”&lt;/em&gt; and came across the Spring documentation on exception handling.&lt;/p&gt;

&lt;p&gt;From my research, I learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Checked exceptions&lt;/strong&gt; must be declared or handled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unchecked exceptions&lt;/strong&gt; like &lt;code&gt;NullPointerException&lt;/code&gt; crash at runtime.&lt;/li&gt;
&lt;li&gt;Spring Boot encourages &lt;strong&gt;centralized error handling&lt;/strong&gt; using &lt;code&gt;@ControllerAdvice&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 4: Implementing the Solution
&lt;/h2&gt;

&lt;p&gt;I implemented a &lt;strong&gt;global exception handler&lt;/strong&gt; to catch errors gracefully and return meaningful responses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="nd"&gt;@RestControllerAdvice&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GlobalExceptionHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NullPointerException&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleNullPointer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NullPointerException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Oops! A required value was missing."&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;BAD_REQUEST&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleGeneralException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Something went wrong: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;✅ Before (Raw Error Log)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;java.lang.NullPointerException:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Cannot&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;invoke&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"String.length()"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;because&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;✅ After (Improved API Response)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Oops! A required value was missing."&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, instead of scaring the client with stack traces, my API gives a &lt;strong&gt;clean and readable message&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Key Takeaways
&lt;/h2&gt;

&lt;p&gt;From this debugging journey, I learned that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exceptions are your &lt;strong&gt;debugging guide&lt;/strong&gt;, not just annoying errors.&lt;/li&gt;
&lt;li&gt;Logging is your &lt;strong&gt;first line of defense&lt;/strong&gt; when debugging backend issues.&lt;/li&gt;
&lt;li&gt;Centralized exception handling with &lt;strong&gt;&lt;code&gt;@ControllerAdvice&lt;/code&gt;&lt;/strong&gt; makes APIs more user-friendly.&lt;/li&gt;
&lt;li&gt;Returning &lt;strong&gt;meaningful error messages&lt;/strong&gt; helps both developers and API consumers.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Community Question
&lt;/h2&gt;

&lt;p&gt;I’d love to hear from you 👇&lt;/p&gt;

&lt;p&gt;👉 Have you faced a tricky &lt;code&gt;NullPointerException&lt;/code&gt; or another runtime error in your Java backend projects? How did you solve it?&lt;/p&gt;

&lt;p&gt;Sharing your debugging approaches could really help beginners like me (and maybe even save someone hours of frustration!).&lt;/p&gt;

</description>
      <category>exceptionhandlinginjava</category>
      <category>nullpointerexceptionfix</category>
      <category>restapierrorhandling</category>
      <category>debugging</category>
    </item>
    <item>
      <title>JVM Architecture and Workflow: Understanding the Java Virtual Machine Internals</title>
      <dc:creator>Mohd Rehan</dc:creator>
      <pubDate>Sat, 06 Sep 2025 20:52:57 +0000</pubDate>
      <link>https://forem.com/bugtobrilliance/jvm-architecture-and-workflow-understanding-the-java-virtual-machine-internals-17pg</link>
      <guid>https://forem.com/bugtobrilliance/jvm-architecture-and-workflow-understanding-the-java-virtual-machine-internals-17pg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Description&lt;/strong&gt;: A beginner-friendly guide to JVM architecture and workflow with real debugging experiences. Learn how the Java Virtual Machine handles code execution, exceptions, and runtime errors step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction: When JVM Internals Became My Debugging Puzzle
&lt;/h2&gt;

&lt;p&gt;As a beginner Java developer, I often thought of the JVM (Java Virtual Machine) as just “&lt;strong&gt;the thing that runs Java programs&lt;/strong&gt;.” That illusion broke when I ran into a &lt;code&gt;OutOfMemoryError: Java heap space&lt;/code&gt; during one of my practice backend projects.&lt;/p&gt;

&lt;p&gt;At first, I thought I had written inefficient code. But the deeper I dug, the more I realized—&lt;strong&gt;the JVM itself decides how memory is allocated, how bytecode is executed, and how exceptions are managed&lt;/strong&gt;. Understanding its internals wasn’t optional anymore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context &amp;amp; Debugging Process
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: The Initial Symptoms&lt;/strong&gt;&lt;br&gt;
While processing a large dataset in Java, my application crashed with:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrva87pqnqdqnvajfak8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrva87pqnqdqnvajfak8.png" alt="an error of OutOfMemoryError" width="800" height="212"&gt;&lt;/a&gt;The error was confusing. Why did my program run fine with small inputs but fail with larger ones?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Debugging with Logs and Tools&lt;/strong&gt;&lt;br&gt;
I enabled** Garbage Collection (GC) logs** using this &lt;strong&gt;JVM&lt;/strong&gt; option:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmha604eyjdg0tll1b2on.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmha604eyjdg0tll1b2on.png" alt="Enabled Garbage Collection (GC) logs" width="800" height="212"&gt;&lt;/a&gt;The logs revealed that &lt;strong&gt;Garbage Collection was running frequently&lt;/strong&gt;, but the heap space was still exhausted.&lt;br&gt;
I then turned to the &lt;code&gt;jconsole&lt;/code&gt; and &lt;code&gt;jvisualvm&lt;/code&gt; tools provided with the JDK. These helped me inspect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heap memory usage&lt;/li&gt;
&lt;li&gt;Thread activity&lt;/li&gt;
&lt;li&gt;Garbage Collector behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Researching JVM Internals&lt;/strong&gt;&lt;br&gt;
I started reading the Java Virtual Machine Specification and several blogs. The key realization was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The JVM architecture (Class Loader, Runtime Data Areas, Execution Engine) controls how Java code is loaded, executed, and managed.&lt;/li&gt;
&lt;li&gt;My issue was directly tied to heap memory management in the Runtime Data Area.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overview of JVM Architecture and Workflow
&lt;/h2&gt;

&lt;p&gt;The JVM is like a mini operating system inside your computer. It provides the environment to execute Java bytecode. Let’s break down its major components:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lx4ba8hm88bowmhifsj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lx4ba8hm88bowmhifsj.jpg" alt="JVM Architecture" width="711" height="776"&gt;&lt;/a&gt;&lt;em&gt;Image source: &lt;a href="https://techvidvan.com/tutorials/wp-content/uploads/sites/2/2020/06/JVM-Model.jpg" rel="noopener noreferrer"&gt;TechVidvan&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Class Loader Subsystem&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loads&lt;code&gt;.class&lt;/code&gt; files into memory.&lt;/li&gt;
&lt;li&gt;Works in three steps: Loading, Linking, and Initialization.&lt;/li&gt;
&lt;li&gt;Example: When you run &lt;code&gt;java MyApp&lt;/code&gt;, the Class Loader loads&lt;code&gt;MyApp.class&lt;/code&gt; into memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Runtime Data Areas&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The JVM divides memory into several regions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Method Area: Stores class-level data like metadata, static variables, and method code.&lt;/li&gt;
&lt;li&gt;Heap: Stores objects created using &lt;code&gt;new&lt;/code&gt;. This is where I faced the &lt;code&gt;OutOfMemoryError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Stack: Each thread gets its own stack storing method frames (local variables, partial results).&lt;/li&gt;
&lt;li&gt;PC Register: Keeps track of the current instruction.&lt;/li&gt;
&lt;li&gt;Native Method Stack: For methods written in other languages (like C).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Execution Engine&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interpreter: Reads and executes bytecode instructions one by one.&lt;/li&gt;
&lt;li&gt;JIT Compiler: Converts frequently used bytecode into native machine code for better performance.&lt;/li&gt;
&lt;li&gt;Garbage Collector: Frees unused objects from the heap to reclaim memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Native Method Interface (JNI)&lt;/strong&gt;&lt;br&gt;
This acts as a bridge between Java code and native libraries (e.g., calling C libraries from Java).&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Example: JVM in Action
&lt;/h2&gt;

&lt;p&gt;Consider this simple code:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feng5356s0nnzxuy6h7oc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feng5356s0nnzxuy6h7oc.png" alt="Sample code for JVM" width="800" height="350"&gt;&lt;/a&gt;&lt;br&gt;
Workflow inside the JVM:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Class Loader loads&lt;code&gt;JvmDemo.class&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Runtime Data Area allocates memory for the &lt;code&gt;message&lt;/code&gt; variable in the heap.&lt;/li&gt;
&lt;li&gt;Execution Engine interprets bytecode and runs &lt;code&gt;System.out.println()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Native Method Interface calls OS-level print functionality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution: Fixing My Heap Space Error&lt;/strong&gt;&lt;br&gt;
To fix the&lt;code&gt;OutOfMemoryError&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Increased heap memory with JVM flags:
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft8e7zqdje05zt3uo3kni.png" alt="Fix OutOfMemoryError" width="800" height="226"&gt;
&lt;/li&gt;
&lt;li&gt;Optimized my code by reusing objects instead of creating unnecessary ones.&lt;/li&gt;
&lt;li&gt;Used profiling tools (&lt;code&gt;jvisualvm&lt;/code&gt;) to track memory leaks and unused references.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JVM architecture isn’t just theory—it directly impacts how your applications run.&lt;/li&gt;
&lt;li&gt;Memory errors like&lt;code&gt;OutOfMemoryError&lt;/code&gt; can be traced back to Runtime Data Areas.&lt;/li&gt;
&lt;li&gt;Debugging tools (&lt;code&gt;jconsole, jvisualvm&lt;/code&gt;) are lifesavers when working with JVM issues.&lt;/li&gt;
&lt;li&gt;Learning GC logs and JVM flags helps you fine-tune performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Community Question
&lt;/h2&gt;

&lt;p&gt;👉 Have you ever debugged a JVM-level issue like memory leaks, GC pauses, or class loading errors? How did you solve it?&lt;/p&gt;

&lt;p&gt;Share your story—I’d love to learn from your experience!&lt;/p&gt;

</description>
      <category>jvm</category>
      <category>java</category>
      <category>javamemorymanagement</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
