Java 21: So How Should We Construct Strings Now?

<p>Java 21 brings in a lot of cool features, and one of them is the String Templates. While it serves more purposes than just classic String interpolation, for us Java developers, it&rsquo;s yet another way to concatenate Strings in a &ldquo;proper&rdquo; way.</p> <p>What is&nbsp;<em>proper,</em>&nbsp;though? I poked around the bytecode and learned some interesting and surprising things about different String concatenation and interpolation techniques in modern Java.</p> <p>I&rsquo;ve also compared it with the Kotlin way (under the hood)&nbsp;</p> <p>But let&rsquo;s begin with Java.</p> <h2><code>+</code>&nbsp;operator</h2> <p>We&rsquo;ve always known that using a&nbsp;<code>+</code>&nbsp;operator is bad practice since Strings are immutable, and under the hood, a new String gets instantiated for every part we concatenate. However, as they say in Dutch, &ldquo;meten is weten,&rdquo; which means &ldquo;measuring is knowing.&rdquo; Let&rsquo;s see what is&nbsp;<em>really</em>&nbsp;happening inside</p> <p><a href="https://betterprogramming.pub/java-21-so-how-should-we-construct-strings-now-772752607d43">Click Here</a></p>