3

Integer, Character, Double, etc. -- all these are immutable classes like String. String has Stringpool to save memory but why don't these wrappers have similar pools?

I have checked: Integer has a similar pool only up to 127, but not more than that.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
Shayan Ghosh
  • 882
  • 6
  • 14
  • 2
    Because `String` by far and away uses the most memory in most applications. To cut down on memory usage for wrapper classes, you can simply use the primitive. – Boris the Spider Apr 19 '15 at 18:38
  • `Character` class holds all the values associated with the standard ASCII character set; `Byte`, `Short` and `Integer`, as you mentioned too, hold the values from -128 to +127. – Mark Apr 19 '15 at 18:56

2 Answers2

5

Unless someone can find a design document from Gosling, et. al., circa 1994 or so that specifically addresses this, it's impossible to say for certain.

One likely reason is that the complexity and overhead weren't deemed worth the benefit. Strings are A) a lot bigger and B) a lot more common than Integer, Long, and such, as mostly people use primitives whenever they can, only using the wrappers where they can't avoid it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

IMO, String is the most commonly used type in java. As an argument to load a class, a param to connect to DB/network connections, to store (almost) each and every thing - the list is long. Usage scenario for rest other primitives/wrapper types combined together would also be negligible compared to String - in any application.

If used in an un-optimized manner (e.g. implemented without Stringpool), performance would be up for a toss - hence it does make sense to have a pool of (only) String.

Raúl
  • 1,542
  • 4
  • 24
  • 37