<div dir="ltr" style="text-align: justify;">In java, string objects are immutable because String objects are cached in String pool. Immutable simply means unmodifiable or unchangeable means once string object is created its data or state can&#8217;t be changed but a new string object is created. JVM has string pool which shared between multiple clients so there is always risk of modification by one client can affect all other clients.It is very important and popular question of java interview, why String is immutable in Java? it is one of the most frequently asked String Interview questions in Java, String class very important class of java and there lots of uses, string class has many benefits because immutable object.</p>
<h2><b>String Pooling requirement</b></h2>
<p>Suppose one client has a string object with value &#8220;java&#8221; if this client changes the value of String &#8220;java&#8221; to &#8220;JAVA&#8221; then all other clients will also see that changed value. As we know JVM provide String pool system to cache the string value for performance reason this risk was avoided by making String class Immutable.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>The following code will create only one string object in the heap.</p>
<pre class="highlight">String str1 = "java"; 
String str2 = "java"; 
</pre>
<p>Here is how it looks:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/string-imutable-1.png" border="0" /></div>
<p>If a string is not immutable, changing the string with one reference will lead to the wrong value for the other references.</p>
<h2><b>Hashing Requirement i.e. caching HashCode</b></h2>
<p>One more thing about string class, it is final class so that no one can override or inherit of String class e.g. Immutability, Caching, hashcode calculation etc by extending and overriding behaviors. Another reason of why String class is immutable could die due to HashMap.</p>
<p>In the HashMap, we can use string as Key of map, even it is very popular to use as key of HashMap. So it is one more reason of string immutablity because we can retrieve the value object which was stored in HashMap via key any where. Since HashMap works in the principle of hashing, which requires same has value to function properly. If suppose string is a Mutable class then String would produce two different hashcodes at the time of insertion and retrieval if contents of String was modified after insertion, potentially losing the value object in the map.</p>
<h2><b>String use in Security</b></h2>
<p>There lots of cases where we use String class widely as a parameter for many java classes, e.g. database connection setting, network connection, opening files, etc. Were String not immutable, a connection or file would be changed and lead to a serious security threat. The method thought it was connecting to one machine, but was not. Mutable strings could cause a security problem in Reflection too, as the parameters are strings.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><b>String Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/how-to-make-java-class-immutable/">How to make Immutable Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-interview-questions/">Core Java Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/string-class-in-java/">String Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/why-string-is-immutable-and-final-in-java/">Why String is Immutable and Final in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-string-to-integer-to-string-in-java-with-example/">Convert String to Integer to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-double-to-string-to-double-in-java-with-example/">Convert Double to String to Double in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-date-to-string-in-java-with-example/">Convert Date to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/differences-between-string-stringbuffer-and-stringbuilder-in-java/">Differences between String, StringBuffer and StringBuilder in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-stringbuilder-and-stringbuffer-in-java/">Differences StringBuffer and StringBuilder</a></b></li>
<li><b><a href="https://dineshonjava.com/how-to-compare-two-strings-in-java/">How to Compare two Strings in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/hashcode-and-equals-methods-in-java/">Difference between hashCode() and equals() methods in Java<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/java-string-substring-method/"> Java &#8211; String substring() Method<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-wait-and-sleep/"> Difference between wait() and sleep()<br />
</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/convert-string-to-integer-to-string-in-java-with-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '72'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…