Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable Classes

 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand String and some wrapper classes of integer etc. are immutable. Can anyone explain the reasoning of making classes immutable? The only thing that I can think of is that the immutable classes are thread safe. Is that the reason?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also the issue of efficiency. Strings are immutable. That allows the system to "re-use" the "same" string object in multiple places - which if it is cranked out alot, can be a big savings.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much more important reasons for String and some wrapper classes be immutable:
Q. What are the compulsive reasons to make a class like String immutable?
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not convinced. Got to be some other or better reasons. If security is the compulsive reason for making String and BigInteger immutable, then why Boolean need to be made immutable?
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, give Dr. Gosling a call, or read here!
Q. Why Java does this? Why Java does that?
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry! I'll violate my own rules one more time.
Boolean, Integer, etc. must be immutable! See Java.lang.reflect package, you will know those are extremely critical.
[ February 01, 2002: Message edited by: Roseanne Zhang ]
 
Frank Lin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that I figured out the reasons. If you take a look at these classes, they are all the basic building blocks of the language, or mostly used classes like Strings. Making these classes immutable can give the compiler maximium capacity to optimize the codes. All these classes are final so they can be inlined without concerns of dynamic binding. Also they can be sure to be thread safe, not just at implementation level, but at the language level, because no operation is allowed to change the value once it is set. This also allows other optimization techniques to be used, such as String Pool pointed out by Cindy, in JVM implementation.
[ February 02, 2002: Message edited by: Frank Lin ]
[ February 02, 2002: Message edited by: Frank Lin ]
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) final class cannot be inherited. Therefore, it is always for static binding. Immutable classes are final, of course, the same here!
2) Boolean/Integer/Long/etc. are not the basic building blocks of Java language. boolean/int/long/etc. are!! If Boolean/Integer/Long/etc. were the basic building blocks of Java language, Java would be dead by now!!! They partially contribute to the slowness of using reflection.
3) Java String class is most famous for its inefficiency. All major coding guideline (including Sun's) will advise use less String class, and use StringBuffer instead. Since whenever you need do something on it, you actually need to build a new one, and put the old one for GC. String literal pooling is just a small remedy for a big disease. Read here for some of the ugliness of Java (String class included): The Java IAQ:Infrequently Answered Questions, read question 12, please. An empty new String() takes 40 bytes!!! That is why C# puts string as a value-type.
I'll stop on this thread, no matter you think How wrong I'm, or what kind of flaming war might happen here. I'm quiting now! However, I'll read!
Thanks!
Roseanne
Need Java real project experience? Join our project team here!
[ February 02, 2002: Message edited by: Roseanne Zhang ]
 
Sasi Kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabu:

Mutable means Changeable ( StringBuffer )
ImMutable means Not Changeable ( String )
For every change that you made to a String will
lead to create a new memory space.
Example:
String test="hai"; // memory is allocated
test="hello"; //now memory of "hai" is left and mew memory for "hello" is created. This "hai" memory will be Garbage collected. But this will not happen in StringBuffer, the same memory will be used.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic