• 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

boolean

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In language fundamentals, RHE it is said that a boolean is represented by 1 bit.
Could someone please explain about the representation of a boolean? I heard it was not 1 bit.
Does anyone know how it works.
Thanks I do very much appreciate the help
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charlie,
In Khalid & Mughal on page 33 it states that the width(bits) of a boolean is not applicable. So I'm not sure the exact size of any boolean, but it is represented by 1 bit. Since a boolean can only be true or false, and a bit can only be 0 or 1, a boolean can be represented by one bit. So for example true can be represented as: 00000001 , and false can be represented as:
00000000.
Like I said, I'm not sure the exact size of a boolean, I just used a byte here to demonstrate what I meant. Obviously the only important bit is the first one, so a boolean can be represented by one bit.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charlie,
Here's what the JVM says about <code>boolean</code> types:


3.3.4 The boolean Type
Although the Java virtual machine defines a boolean type, it only provides very limited
support for it. There are no Java virtual machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java virtual machine int data type.
The Java virtual machine does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore.2
The Java virtual machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.


From the above, looks as if it is handled as an <code>int</code> in most cases; a <code>byte</code> for arrays.
I don't think it would be practical to handle it as a <code>bit</code> because of memory addressing; which is usually 8-bit, 16-bit, 32-bit or 64-bit.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 21, 2001).]
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks that was a great answer.
I had another question in regards to boolean. If one uses the RandomAccessFile method read() which reads bytes of data, how many bytes would I read.
As per the above, if it was stored as anything but an array of bytes, I would read 4 bytes and the last byte would have the boolean value. Am I on the right track. (assuming I did not use readBoolean)
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charile,
Apologies for the late response.
I tried the following to see what would happen:
<br /> It returns the following output:<br />
<code>read()</code> uses 8-bits or one byte; so seems to work ok however if you throw another type into the mix it can get messy.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 26, 2001).]
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for the complete answer.
Sincerely,
Charlie
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic