Home

Jobs

Blogs

Contact us
Verwin Forum Ask Question
Verwin User Default Image
Being a computer science engineer and a java developer, I am now preparing to clear Java coding certification course. I want to know the reason behind why String class is immutable in JAVA?
Verwin Posted Date Posted on 22/12/2019
Verwin Count 1 Answers
Verwin User Default Image Vikram S Verwin Posted DateAnswered on 24/12/2019
String literals are cached in a string pool and this saves a lot of heap space because different string variables refer to the same object in a string pool . This is possible only when Strings are immutable.
Also sensitive information like passwords etc. are stored as String variables and if they are mutable it poses huge security threat.
These are the major reasons to keep String class immutable.
Being a computer science engineer and a java developer, I am now preparing to clear Java coding certification course. I want to know the reason behind why String class is immutable in JAVA?
String literals are cached in a string pool and this saves a lot of heap space because different string variables refer to the same object in a string pool . This is possible only when Strings are immutable.
Also sensitive information like passwords etc. are stored as String variables and if they are mutable it poses huge security threat.
These are the major reasons to keep String class immutable.
Verwin User Default Image
Answered on: 24/12/2019