• 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

Size of boolean .

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all ,
now i'm a little cofused over this one . what's the size of boolean in memory?
i know that it's JVM dependent but then why some people say that it takes 1 byte while others say that it doesn't take any memory at all ? i'll also quote something from just java 2 here .

"boolean

This is the data type used for true/false conditions. To speed up memory access, implementations don't pack boolean values into the theoretical one-bit minimum space, but put each boolean into a byte.
.
.
.
Unlike some other languages, the Java boolean type is not based on integers. In particular, the Java programmer cannot increment, decrement, shift, or add boolean values. Inside a JVM, however, there are no instructions specifically for booleans, so integer operations (like assigning zero) are used. In the Sun JVM, the byte containing a boolean is promoted to 32 bits when pushed onto the stack during execution. So don't think booleans are a 1-bit type that economizes on storage."

what is the author trying to say ?
 
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
He's saying that booleans are mostly stored as ints. It's possible that in some JVM implementations, an array of boolean might be stored as an array of bytes or even a bit field, but there's really no way to know other than by looking at the source code for the JVM you're using.

He should also be saying don't worry about it. This kind of behind-the-scenes information is rarely useful, especially to the Java beginner.
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i know that i don't have to _worry_ about it but doesn't hurt to know stuff , you would agree . one more thing ( and i've read it in many places like the sun's developer forums ) - why is it that an array of boolean values are stored as bytes ( one byte for each boolean value ) and otherwise the memory it takes is JVM dependent ? thanking in anticipation . Vaib .
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Sun thought that would be a good idea, when balancing performance (which suggests using whole words for booleans) against storage requirements (which suggests using the smallest possible booleans).

A good thing about Java is that you are shielded from the differences between different platforms and JVMs. It is incredibly rare to need to know how big any particular data item is, in Java.

On the extremely rare occasions when you do need to know, there's no programmatic way to find out. You may get an answer from experiment or documentation, but it will be specific to one JVM implementation on one platform.
 
Marshal
Posts: 28134
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vaibhav panghal:
yes i know that i don't have to _worry_ about it but doesn't hurt to know stuff , you would agree .

I'm not sure I agree with that. 10 years ago there were a lot of surveys that showed that Java was "slower" than some other languages. There are still a lot of people who "know" that information and base decisions on it. What I'm saying here is that there is useful information and there is useless information and there is information that appears to be useful but actually isn't.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's more important that you understand the possible ways in which booleans might be implemented - i.e. the basics of computer science. And know that the JVM can decide itself how to implement them.

That the architecture of the byte-code (the same for all JVMs) might direct most implementer in a certain direction.

That there is no way to programmatically ensure the implementation, and that you should therefor not depend on an implementation.

And that if fiddling bits is an important aspect of the program you need to write, then Java is possibly a bad candidate for your solution.

and that there is always the next thing that you don't or can't know - figure this one out, and it will come along, �
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much all of you . i think i got the concept from the discussion here .
 
reply
    Bookmark Topic Watch Topic
  • New Topic