• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Boolean Objects are not mainted in cache?  RSS feed

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just writing the code and someone pointed out that


wastes memory and taking the intelligence of the compiler I replied that it don't as Boolean can have two options. Either true or false.

To verify it I checked the source code of Boolean class and found astonishing results. Every time we create a Boolean Object a new Object is created. But why do we need a new Object? Lets say we can have two Objects in cache as immutable and whenever we need one a reference to them will be given just like Integer class where a cache is maintained of integers from -128 to +127.

Are there any specific reasons why they have gone for this type of implementation? If someone is aware then please reply.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look for Boolean in the API and look for "fields."

When you use the new operator you are telling the compiler you need to use enough memory for one more object.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. But my question is why are they not going to reuse the two final Boolean Objects already present there.


Anyway we can have only one value out of two. Then what is the need of going for new Object? Why cant we just use these Objects?

 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be a dubious bit of design, but some of these immutable classes do allow duplicate objects to be created. You can do the same with Strings, eg new String("Campbell"). The fact that a feature is available does not mean you have to use it, but in library classes they tend to provide all sorts of features just in case anybody might ever want to use them.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. But my question is why are they not going to reuse the two final Boolean Objects already present there.


That's the point. Instead of writing:

Boolean b = new Boolean(true);

if you want to use the cached versions you have to write one of these:

Boolean b = Boolean.TRUE;
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = true; //Autoboxing version

Or are you asking why public constructors are exposed at all in the Boolean class? I would suspect that it's for symmetry with the other primitive "wrapper" classes that all expose public constructors for object creation.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Gupta wrote:To verify it I checked the source code of Boolean class and found astonishing results. Every time we create a Boolean Object a new Object is created. But why do we need a new Object? Lets say we can have two Objects in cache as immutable and whenever we need one a reference to them will be given just like Integer class where a cache is maintained of integers from -128 to +127.

If you use the new operator, then a new object is created - always. This is also true for Integer objects. If you do this:

then a new Integer object will be created - this will not reuse an Integer object from the cache. The cache for Integer is only used when you use the Integer.valueOf(int) method, or in the case of autoboxing (because autoboxing uses Integer.valueOf(int) behind the scenes).

In the case of Boolean, there are only two values, which are defined as constants in the Boolean class: Boolean.TRUE and Boolean.FALSE. Use those two constants, or use Boolean.valueOf(boolean), which will return one of the two constants.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you can also say

Boolean b = true;

and auto-boxing will use those constant fields!

In any event: don't ever write "new Boolean(true)", just as you should never, ever write 'new String("anything")'. Both are just silly, and if you find this in code, you can be sure the programmer was a newbie.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the valuable inputs from all of you. It is clear now.

What I think is if they would have made it's constructor private and a method to get Objects that must have saved some memory. Same with wrapper classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!