Core Java Interview Questions

Why String is Immutable and Final in Java

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’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.

String Pooling requirement

Suppose one client has a string object with value “java” if this client changes the value of String “java” to “JAVA” 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.

The following code will create only one string object in the heap.

String str1 = "java";
String str2 = "java";

Here is how it looks:

If a string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

Hashing Requirement i.e. caching HashCode

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.

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.

String use in Security

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.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago