• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Boolean Objects are not mainted in cache?

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic