<?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>DEV Community: Md Marzukul Islam</title>
    <description>The latest articles on DEV Community by Md Marzukul Islam (@marzuk16).</description>
    <link>https://dev.to/marzuk16</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%2F1242839%2Fd660cd10-bce2-4551-879a-b392f5bcb875.jpg</url>
      <title>DEV Community: Md Marzukul Islam</title>
      <link>https://dev.to/marzuk16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marzuk16"/>
    <language>en</language>
    <item>
      <title>Understanding Integer Caching in Java, The Hidden Optimization You Might’ve Missed</title>
      <dc:creator>Md Marzukul Islam</dc:creator>
      <pubDate>Mon, 10 Nov 2025 10:03:59 +0000</pubDate>
      <link>https://dev.to/marzuk16/understanding-integer-caching-in-java-the-hidden-optimization-you-mightve-missed-ggi</link>
      <guid>https://dev.to/marzuk16/understanding-integer-caching-in-java-the-hidden-optimization-you-mightve-missed-ggi</guid>
      <description>&lt;p&gt;Ever wondered why sometimes two seemingly identical Integer objects in Java are actually the same object in memory, and sometimes they aren’t?&lt;/p&gt;

&lt;p&gt;Welcome to the fascinating world of Integer Caching, a small but clever optimization built right into the Java Virtual Machine (JVM) that quietly boosts performance and saves memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is &lt;code&gt;Integer Caching&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;In Java, objects like &lt;code&gt;Integer&lt;/code&gt;, &lt;code&gt;Long&lt;/code&gt;, &lt;code&gt;Byte&lt;/code&gt;, and &lt;code&gt;Character&lt;/code&gt; are &lt;strong&gt;immutable wrapper classes&lt;/strong&gt; around primitive types. Whenever you use autoboxing, such as:&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="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// autoboxing of int to Integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the JVM doesn’t always create a &lt;em&gt;new&lt;/em&gt; &lt;code&gt;Integer&lt;/code&gt; object. Instead, it might reuse an existing one, thanks to &lt;strong&gt;Integer caching&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;p&gt;By default, Java maintains a &lt;strong&gt;cache of Integer objects&lt;/strong&gt; for values between &lt;strong&gt;-128 and +127&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So when you write:&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="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; refer to the &lt;strong&gt;same cached object&lt;/strong&gt; from the JVM’s internal pool.&lt;/p&gt;

&lt;p&gt;However, when you go beyond this range:&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="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;128&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="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F5eeoam6ylqv0nnqlvzz7.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%2F5eeoam6ylqv0nnqlvzz7.jpg" alt=" " width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each value gets a &lt;strong&gt;new Integer object&lt;/strong&gt;, since &lt;em&gt;128&lt;/em&gt; is outside the default cache range.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why -128 to 127?
&lt;/h2&gt;

&lt;p&gt;This range was chosen because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These values are commonly used (especially in loops, small counters, array indexes, etc.).&lt;/li&gt;
&lt;li&gt;-128 to 127 fits neatly within a single byte (&lt;code&gt;Byte&lt;/code&gt; range).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This small range gives a big payoff, fewer object allocations and faster comparisons for the most frequently used numbers.&lt;/p&gt;

&lt;h1&gt;
  
  
  JVM Customization For Caching Range
&lt;/h1&gt;

&lt;p&gt;The upper limit of the cache can actually be &lt;strong&gt;customized!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can modify it using the JVM option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;    &lt;span class="py"&gt;-Djava.lang.Integer.IntegerCache.high&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;value&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, if your application heavily uses numbers up to &lt;code&gt;1000&lt;/code&gt;, you could extend the cache like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;    &lt;span class="py"&gt;-Djava.lang.Integer.IntegerCache.high&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  A Common Pitfall
&lt;/h1&gt;

&lt;p&gt;It’s important to remember that &lt;code&gt;==&lt;/code&gt; &lt;strong&gt;compares object references&lt;/strong&gt;, not values.&lt;/p&gt;

&lt;p&gt;That’s why you might see confusing results:&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="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true (cached)&lt;/span&gt;

    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;128&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="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false (not cached)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When comparing numeric values, always use &lt;code&gt;.equals()&lt;/code&gt; instead:&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="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="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>jvm</category>
      <category>performance</category>
      <category>techexplained</category>
    </item>
    <item>
      <title>A Tiny Decimal That Caused Big Trouble, Until I Learned About BigDecimal</title>
      <dc:creator>Md Marzukul Islam</dc:creator>
      <pubDate>Mon, 03 Nov 2025 07:02:35 +0000</pubDate>
      <link>https://dev.to/marzuk16/a-tiny-decimal-that-caused-big-trouble-until-i-learned-about-bigdecimal-30ji</link>
      <guid>https://dev.to/marzuk16/a-tiny-decimal-that-caused-big-trouble-until-i-learned-about-bigdecimal-30ji</guid>
      <description>&lt;p&gt;If you’ve ever wondered why developers always say “never use double for money”, here’s the real reason, and why BigDecimal solves the problem perfectly.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Root Cause: &lt;code&gt;Binary&lt;/code&gt; VS &lt;code&gt;Decimal&lt;/code&gt; Representation
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;double&lt;/code&gt; in Java uses &lt;strong&gt;IEEE 754 binary floating point&lt;/strong&gt;, meaning it represents numbers as sums of powers of &lt;strong&gt;2&lt;/strong&gt;, not &lt;strong&gt;10&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That works fine for many numbers, but not for decimals like &lt;code&gt;0.1&lt;/code&gt; or &lt;code&gt;0.2&lt;/code&gt;, because they can’t be represented exactly in binary.&lt;br&gt;
They become repeating binary fractions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0.1₁₀ = 0.0001100110011001100...₂ (infinite)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this simple code:&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="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="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0.30000000000000004
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That tiny error is a &lt;em&gt;rounding artifact&lt;/em&gt;, and in finance or accounting, it’s unacceptable.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. How &lt;code&gt;BigDecimal&lt;/code&gt; Fixes It
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;BigDecimal&lt;/code&gt; doesn’t use floating-point math at all.&lt;br&gt;
Internally, it stores two things:&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="nc"&gt;BigInteger&lt;/span&gt; &lt;span class="n"&gt;intVal&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// exact integer value&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// how many digits after the decimal point&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&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="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;x&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;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"123.45"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is stored as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    intVal = 12345
    scale  = 2
    value  = 12345 × 10^-2 = 123.45
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which is 100% exact, no rounding errors.&lt;/p&gt;

&lt;p&gt;That’s because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BigInteger&lt;/code&gt; holds &lt;strong&gt;whole numbers&lt;/strong&gt; precisely in binary.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;scale&lt;/strong&gt; simply shifts the decimal point in base &lt;strong&gt;10&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Why &lt;code&gt;BigDecimal&lt;/code&gt; Is Perfect for Money
&lt;/h2&gt;

&lt;p&gt;Money works in decimal, not binary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BigDecimal&lt;/code&gt; uses &lt;strong&gt;decimal arithmetic&lt;/strong&gt;, so values like &lt;code&gt;0.1&lt;/code&gt;, &lt;code&gt;19.99&lt;/code&gt;, or &lt;code&gt;123456.78&lt;/code&gt; are stored and calculated exactly as written, no binary drift, no rounding surprises.&lt;/p&gt;

&lt;p&gt;Yes, it’s slower than &lt;code&gt;double&lt;/code&gt;, but it’s correct and correctness is what matters in financial systems.&lt;/p&gt;

</description>
      <category>java</category>
      <category>bigdecimal</category>
      <category>fintech</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
