World of tricky Core Java Q and A Covering Object Oriented Analysis and Design, JVM Internals,Java Language Fundamentals,Datatypes,Keywords,Operators and Assignments,Identifies,Declarations and Modifiers, Conversion,Casting and Promotion, Flow control,Assertions, Exception Handling and Garbage Collection,Objects and Classes), Basic Packages and their classes,JDBC,JFC Swing,Java Server Pages (JSP), Servlets,EJB,JMS,JNDI etc and Open source technologies like Struts,Hibernate,Spring etc
Search This Blog
Tuesday, August 01, 2006
Why String class is immutable?
JVM internally maintains the "String Pool". To achive the memoryefficiency, JVM will refer the String object from pool. It will notcreate the new String objects. So, whenevr you create a new stringliteral, JVM will check in the pool whether it already exists or not.If already present in the pool, just give the reference to the sameobject or create the new object in the pool. There will be manyreferences point to the same String objects, if someone changes thevalue, it will affect all the references. So, sun decided to make itimmutable.
Subscribe to:
Post Comments (Atom)
2 comments:
+ some security reasons I think :)
nice blog anyway, I will add it to my links
Here are few more reason for string immutability.
http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html
Post a Comment