• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Wrapper Classes are Immutable  RSS feed

 
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason that all wrapper classes are immutable? Is it only for the reason that they can be used as HashMap Keys or do we have some other benefits as well?
 
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "wrapper classes", and why do you think the are all immutable?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess he means the standard wrapper classes for primitives: java.lang.Integer, java.lang.Long, etc., and yes, they are immutable.

Making classes immutable has many advantages, for example with regard to multi-threaded programming, and makes it possible to do optimizations such as caching, which auto-boxing makes use of.
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jesper, Tim, by Wrapper classes, I mean standard wrapper classes for primitives like Integer, Boolean etc.

Could you please explain how it helps in Caching, auto-boxing etc.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav Gargs wrote:
Could you please explain how it helps in Caching


Well, the purpose of caching is to facilitate sharing. So, if you have a dozen places in your application that needed to have the Integer instance with a value of 42, then you can use only one instance instead.  However, if Integer instances were mutable, then how would it work? How would you share these instances, if all the dozen places can change the value?

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 116
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Do we mean to say this is same as String Pool? Do we have pool for all immutable objects?

Thanks.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Immutable types may be pooled, but only few are.

String pools literals and instances on which you call intern().

Byte, Short and Integer pool values representing -128 to 127. Character pools values representing '\u0000' to '\u007F'.

When you write an immutable type, you can also choose to let it pool certain values. For instance, consider a Card class from a deck of playing cards:

The first time you call Card.of(ACE, SPADES), it will create a new Card instance, and store it in the CARDS pool. The second time you can Card.of(ACE, SPADES), it will return the instance it stored in the pool.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Byte, Short and Integer pool values representing -128 to 127. Character pools values representing '\u0000' to '\u007F'.

With Oracle's JRE (possibly others as well), Long provides the same caching, and only Float and Double don't support any caching.
Also with Oracle's JRE, the Integer cache can be increased on the positive side using system property java.lang.Integer.IntegerCache.high. This must be set before the Integer class is loaded, which more or less means you must set it when you start the JVM (using -Djava.lang.Integer.IntegerCache.high=<value>).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!