Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (24 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (322 Blogs)
  • Spring Framework (8 Blogs)

Immutable String in Java: All you Need to Know

Published on Oct 15,2019 1.1K Views

Dealing with Immutable String in Java is a very important Aspect in any programming language. In this article, we will discuss the following pointers:


Introduction to Immutable String in Java

The string pool has string objects that are cached due to which string is immutable. Multiple clients share the cached string literals. The risks involved in the action of one client would affect all the other clients.

For example, when a client changes the String value “Test” to “TEST”, all other clients will see the changed value. As string objects caching was important for performance, the Immutable String class avoided the risk. Also, String was made final so that the invariant of the String class will not be compromised. Example Immutability, Caching, hash code calculation done by extension and override of behaviors. 


Hashmap Key

The HashMap key is very popular, immutability is very important so that the value object can be retrieved from HashMap. Hashing is the principle of HashMap, which requires the same has value to function properly. Two different hashcodes are produced one at the time of insertion and other at the time retrieval when contents are modified after insertion, potentially losing map value object in the map.

Hashtable-Immutable String in Java
Immutable String class in Java is the most frequently asked Interview questions. The discussion starts with, what is a String, how it is different from String in C++, and then the question follows are immutable objects in Java, benefits of the immutable object, what is their use and the scenarios to use them. 


Need for Immutable String in Java

Two reasons are important to say why Immutable String class is made in Java: 

1. It is not possible at all in the case to have one string object/literal.

Let us refer to below example: 

“Test” can be referred by many reference variables. If any one of them changes the value for others, this will be affected as well. Let us see how.

String A = "Test"
String B = "Test"

String A called, “Test”.toUpperCase() which changes the same object into, "TEST" so B will also be “TEST” which is not needed.  Below is a diagram that tells us how String is created in heap memory and String literal pool.

immutable in java


2. The string had widely been used as a parameter for many Java classes. For example, when we open a network connection, we can pass hostname and port number as a string, for opening database connection we can pass the database URL as a string. 

An immutable string would lead to serious security threats. For example, someone can access a file, to which he has access and then change the name of the file name to gain access to the other file. In the case of immutability, no need to worry about these threats. 

An immutable string can safely share threads as well, which is important for multithreaded programming. An immutable string instance is thread-safe in Java so no need to synchronize String operation externally to avoid synchronization issues in Java. Another important point to note is the memory leak caused by Substring, which is not a thread related issue but something to be aware about.

With this, we come to the end of this “Immutable String in Java” article. Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.