Q3. Why String class has been made immutable in Java? A4. For performance & thread-safety. 1. Performance: Immutable objects are ideal for representing values of abstract data (i.e. value objects) types like numbers, enumerated types, etc. If you need a different value, create a different object. In Java, Integer, Long, Float, Character, BigInteger and BigDecimal are all immutable objects. Optimisation strategies like caching of hashcode, caching of objects, object pooling, etc can be easily applied to improve performance. If Strings were made mutable, string pooling would not be possible as changing the string with one reference will lead to the wrong value for the other references.
AnswerBot
1y
String class is immutable in Java for performance and thread-safety.
Immutable objects are ideal for representing values of abstract data types
Optimization strategies like caching of hashcode, caching ...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Trainee
>
Vidushi Infotech Software Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app